Inherited values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Inherited values

    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:

    Code:
    		var dDept = new Object();
    		var vDept = aPhaseCodes[0].substring(0,4);
    
    		var pOptions = new Array();
    		for (var i = 0; i < aPhaseCodes.length; i++ )
    		{
    			var pCode = aPhaseCodes[i].substring(5,7);
    	
    			if (vDept == aPhaseCodes[i].substring(0,4))
    			{
    				pOptions[pOptions.length] = {value:pCode,text:pCode};
    			}else{
    				dDept[vDept] = pOptions;
    //problem occurs here
    				pOptions.length = 0;
    				vDept = aPhaseCodes[i].substring(0,4);
    				pOptions[pOptions.length] = {value:pCode,text:pCode};
    			}
    		}
    		dDept[vDept] = pOptions;
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    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

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      To avoid this problem, use the slice() method.

      Comment

      • rnd me
        Recognized Expert Contributor
        • Jun 2007
        • 427

        #4
        Originally posted by gits
        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

        use the [].concat shortcut instead of slow looping:
        Code:
        var a = [1,2,3,4]
        var b = [].concat(a);
        b[2] = "hello world";
        alert(a) // 1,2,3,4

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          that's a nice idea for cloning ... and it works for arrays. smart & cool ;)

          kind regards

          Comment

          • Claus Mygind
            Contributor
            • Mar 2008
            • 571

            #6
            Originally posted by rnd me
            use the [].concat shortcut instead of slow looping:
            Code:
            var a = [1,2,3,4]
            var b = [].concat(a);
            b[2] = "hello world";
            alert(a) // 1,2,3,4
            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

            Comment

            • rnd me
              Recognized Expert Contributor
              • Jun 2007
              • 427

              #7
              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.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                i found a problem with the shown 'clone'-method ... have a look at the following example:

                [code=javascript]
                var a = [1,[1,2],3,4,3,3,4,5];

                var b = [].concat(a);

                b[1].length = 0;

                alert(a);
                [/code]
                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

                Comment

                • Claus Mygind
                  Contributor
                  • Mar 2008
                  • 571

                  #9
                  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

                  Comment

                  Working...