By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

Inherited values

Claus Mygind
100+
P: 571
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
Share this Question
Share on Google+
8 Replies


gits
Expert Mod 5K+
P: 5,390
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
Expert Mod 15k+
P: 16,027
To avoid this problem, use the slice() method.
Nov 24 '08 #3

rnd me
Expert 100+
P: 427
@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
Expert Mod 5K+
P: 5,390
that's a nice idea for cloning ... and it works for arrays. smart & cool ;)

kind regards
Nov 24 '08 #5

Claus Mygind
100+
P: 571
@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
Expert 100+
P: 427
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
Expert Mod 5K+
P: 5,390
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
100+
P: 571
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

Post your reply

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