0

Given this fiddle, does anyone have a suggestion as to how I might update the indices of array1? Or more to the point, any idea how to make the indices of array2 references to indices of array1?

http://jsfiddle.net/y8rs56r3/

    var array1 = [
        {num:"one"},
        {num:"two"},    
        {num:"three"}
    ];
    var array2 = [];
    var i = array1.length;
    while(i--){
        if(i!=1)array2.push(array1[i]);
    }

    array2[0].num = "one updated";
    console.log(array2);
    console.log(array1);

Obviously, in this codeblock, array1[0] is not updated.

7
  • 2
    What do you want to accomplish? By pushing as you have done, you have reversed the order of the elements (as well as excluded element i from array1). As it stands, array1[2] is the same object as array2[0] and is, in fact, updated in array1 as well as in array2. If you don't want to reverse, iterate in a forward direction. Commented Dec 17, 2014 at 17:36
  • @TedHopp, that's correct. array2 isn't just a clone of array1, or any given subset of consecutive indices of it. In the actual application, array2's indices can be anything from 0 to all of the indices of array1. Commented Dec 17, 2014 at 17:41
  • 1
    Well, that doesn't answer my main question: what do you want the behavior to be? Commented Dec 17, 2014 at 17:41
  • 2
    Your last statement is incorrect. Element array1[2] is updated, at least when I ran your fiddle. Commented Dec 17, 2014 at 18:04
  • 1
    Yep. As your loop is starting from the end the index 2 of the array1 is changed from {num:"three"} to {num:"one updated"}. Commented Dec 17, 2014 at 18:19

1 Answer 1

1

since your array is set of objects try like this:

  var array1 = [
              {num:"one"},
              {num:"two"},    
              {num:"three"}
          ];
       var array2 = [];
         for(x in array1){
                   array2.push(array1[x]);
                    }
          array2[0].num = "one updated";
          console.log(array2);//output  [Object { num="one updated"}, Object { num="two"}, Object { num="three"}]
          console.log(array1);// output  [Object { num="one updated"}, Object { num="two"}, Object { num="three"}]
Sign up to request clarification or add additional context in comments.

3 Comments

Interesting. Why does this work when the while loop doesn't? Also, if I'm using this a lot in the context of a large application, potentially looping over large collections, should I start to worry about performance of the for in loop?
if you will assign objects they will reflect the changes to their references also.there might be an easy way but this is what i come up with.wait for some time and see if some other posts are coming.
to answer why it did not work in while, since you never assigned the first element of array1 to array2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.