We help IT Professionals succeed at work.

Copy an array of an array, not by reference.

Conrado ZAVALA
on
141 Views
Last Modified: 2014-09-02
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.
Comment
Watch Question

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:

Author

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.
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you very much.
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

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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.
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.