473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Inherited values

Claus Mygind
571 512MB
I have a problem in my code, I accumulate a dataset into an object "pOptions" Then I add the contents of this object into an array of another object "dDept[vDept]". Next I want to reset the value of the first object "pOptions", so new values can be accumulated and transferred into the 2nd object "dDept[vDept]". But when I reset the value of the first object "pOptions" to blank then the 2nd array object "dDept[vDept]" is also wiped out. How can I prevent this. Here is my code:

Expand|Select|Wrap|Line Numbers
  1.         var dDept = new Object();
  2.         var vDept = aPhaseCodes[0].substring(0,4);
  3.  
  4.         var pOptions = new Array();
  5.         for (var i = 0; i < aPhaseCodes.length; i++ )
  6.         {
  7.             var pCode = aPhaseCodes[i].substring(5,7);
  8.  
  9.             if (vDept == aPhaseCodes[i].substring(0,4))
  10.             {
  11.                 pOptions[pOptions.length] = {value:pCode,text:pCode};
  12.             }else{
  13.                 dDept[vDept] = pOptions;
  14. //problem occurs here
  15.                 pOptions.length = 0;
  16.                 vDept = aPhaseCodes[i].substring(0,4);
  17.                 pOptions[pOptions.length] = {value:pCode,text:pCode};
  18.             }
  19.         }
  20.         dDept[vDept] = pOptions;
  21.  
Nov 24 '08 #1
8 1217
gits
5,390 Expert Mod 4TB
that's right ... arrays and objects are 'passed by reference' ... when you assign pOptions to dDept[vDept] then you pass the reference to the array and dDept[vDept] points to the array now ... you would need to clone the array value by value in case you want to 'copy' it ...

kind regards
Nov 24 '08 #2
acoder
16,027 Expert Mod 8TB
To avoid this problem, use the slice() method.
Nov 24 '08 #3
rnd me
427 Expert 256MB
@gits

use the [].concat shortcut instead of slow looping:
Expand|Select|Wrap|Line Numbers
  1. var a = [1,2,3,4]
  2. var b = [].concat(a);
  3. b[2] = "hello world";
  4. alert(a) // 1,2,3,4
Nov 24 '08 #4
gits
5,390 Expert Mod 4TB
that's a nice idea for cloning ... and it works for arrays. smart & cool ;)

kind regards
Nov 24 '08 #5
Claus Mygind
571 512MB
@rnd me
Ok I like this example now how do I make 1,2,3,4 into variables [var1,var2,var3,var4]?

I have seen the example but always been stumped by the fact the data is shown as a constant and not as a variable.

When I receive the data back from my ajax call I split it into an array aPhaseCodes[0]. Where the data takes this format 9999_99 for each element in the array hence the use of the .substring(0,4) and .substring(5,7). In the example below for example the first two elements would look like this 1099_10 and 1099_15

I want to build an array of objects with a structure something like this (see below) which I use to build a select box on the user's screen (form). Depending on which department the employee belongs to, those will be the options values loaded into the the select box.

dDept
1099
0
value 10
text "something"
1
value 15
text "something"
1110
0
value 80
text "something"
2230
0
value 30
text "something"
If I can make this work then I will have to attempt to use the slice() method, not as palatable.

The best option of course would be to assemble a string on the server side of the ajax call that could easily be transformed into the array I want. But that has eluded me to this point.

Thanks for all the help and suggestions
Nov 25 '08 #6
rnd me
427 Expert 256MB
im a little confused. can you post an example raw response (before any splitting)?

i could/would easily create the ob array you detail if i knew the exact input.

redact it if you must, but preserve the format.
Nov 25 '08 #7
gits
5,390 Expert Mod 4TB
i found a problem with the shown 'clone'-method ... have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. var a = [1,[1,2],3,4,3,3,4,5];
  2.  
  3. var b = [].concat(a);
  4.  
  5. b[1].length = 0;
  6.  
  7. alert(a);
  8.  
so when directly changing an obj/array within the 'copy' the same problem as the original one occurs since the reference to the contained obj was copied. so the shown method obviously just works reliable for 'plain arrays' that just contain primitive values ...

kind regards
Nov 25 '08 #8
Claus Mygind
571 512MB
I appreciate all the great suggestions and offers of help. But this ordeal has made me rethink the process. I was downloading more data than needed, creating an array to fill a list box with options.

I am going to cut out the middle man "that array" which I had trouble building and only download the data needed, from there create the list box directly.

Thanks again for all the support
Nov 26 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Josh Mcfarlane | last post by:
I'm doing recomposition of objects from binary streams, and the best way for me to write them out is to write base class data first, forward to inherited classes, pointer class values, etc. Now,...
0
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
9
by: Alex | last post by:
I have a serious problem and I hope there is some solution. It is easier to illustrate with a simple code: >>> class Parent(object): __slots__= def __init__(self, a, b): self.A=a; self.B=b...
6
by: Andreas | last post by:
Hello list, what about uniqueness of inherited primary keys ? eg you have : create table objects ( id int4, date_created timestamp(0), primary key (id)
2
by: Tamir Khason | last post by:
I have a VERY COMPLICATED MULTILINGUAL form with base values on it I want to produce about 100 forms like this, BUT with their own values and properties, so following IDE flow I make derrived form...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
2
by: Scott Goodwin | last post by:
In the following example: create table parent ( id integer unique not null, name varchar(24) ); create table child ( first_name varchar(256), last_name varchar(256)
4
by: Sebastian Böck | last post by:
Hello all, i have a view defined as a simple select of a table. This table is inherited by a couple of others. All entries belong to the child-tables. I also have an unconditional update rule...
5
by: Shak | last post by:
Hi all. I was led to believe that static methods were not inherited by their subclasses (and since that makes sense, rightly so). However, a subclass I've written is using it's (abstract)...
16
by: gwoodhouse | last post by:
Good Morning, Ive been programming in c# for a few months now, and one of the things i havnt quite figured out is this: I would like to have inherited classes with their own set of variables....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.