• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 121
  • Last Modified:

Copy an array of an array, not by reference.

Dear Experts,

I have an array (parentarray) that contains another array (childarray) and I need to copy the array that is the child of my parent array.
For example:

parentarray[0].fname="Joe";  
parentarray[0].lname="Smith";
parentarray[0].childarray[0].eyescolor="blue";
parentarray[0].childarray[0].haircolor="blue";

I need to copy to a new array just the childarray but not by reference, I need an exact copy of the child array.
How could this be done?

Thanks in Advance.
0
Conrado ZAVALA
Asked:
Conrado ZAVALA
  • 4
  • 2
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
parentarray[0].fname="Joe";  
parentarray[0].lname="Smith"; 
parentarray[0].childarray[0].eyescolor="blue";
parentarray[0].childarray[0].haircolor="blue";

var newChild = {};
for(var j in parentarray[0].childarray) {
   newChild[j] = parentarray[0].childarray[j];
}

// checking :
alert(JSON.stringify(newChild));

Open in new window

0
 
leakim971PluritechnicianCommented:
0
 
Conrado ZAVALAAuthor Commented:
Hi leakim971,

I know the length of the child array, let's suppose it is 3 (the child array is from 0 to 2) and I need to copy just the last element of that array.

How can use that code for copying just the last element of the array.

Thanks in Advance.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
leakim971PluritechnicianCommented:
Like this :
var newChild = {};
var numberOfChildrens = parentarray[0].childarray.length;
for(var j in parentarray[0].childarray[numberOfChildrens-1]) {
   newChild[j] = parentarray[0].childarray[numberOfChildrens-1][j];
}

Open in new window


Test page : http://jsfiddle.net/0knyc4Lz/6/
0
 
Conrado ZAVALAAuthor Commented:
Thank you very much.
0
 
Randy PooleCommented:
just fyi you can use slice which does the same thing without the loop;
var newChild=parentarray[0].childarray.slice(0);

Open in new window

0
 
leakim971PluritechnicianCommented:
test page : http://jsfiddle.net/u60fx11c/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

Description
slice does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:

For object references (and not the actual object), slice copies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.
0
 
Randy PooleCommented:
For strings and numbers (not String and Number objects), slice copies strings and numbers into the new array. Changes to the string or number in one array does not affect the other array.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now