Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Copy an object of an array in the same array?

Posted on 2014-08-30
7
Medium Priority
?
207 Views
Last Modified: 2014-09-01
Dear Experts,

I was wondering on how to make a exact copy of an object (index) of an array inside the same array.
For example:

I got this array:
array[0] = { id: gr, name: green, number: 1};
array[1] = { id: rd, name: red, number: 2};
array[2] = { id: bl, name: blue, number: 3};

and I want to make an exact copy of array[2] in the same array.
In other words, what I want to do is to add 1 object to the array (array[3]) with exactly the same info of array[2].

so at the end the array will be:
array[0] = { id: gr, name: green, number: 1};
array[1] = { id: rd, name: red, number: 2};
array[2] = { id: bl, name: blue, number: 3};
array[3] = { id: bl, name: blue, number: 3};

It's that possible?

Thanks in Advance.

Best Regards.
0
Comment
Question by:Conrado ZAVALA
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 1400 total points
ID: 40294564
test page : http://jsfiddle.net/a16x1j88/1/
var array = [];
array[0] = { id: "gr", name: "green", number: 1};
array.push({ id: "rd", name: "red", number: 2});
array[2] = { id: "bl", name: "blue", number: 3};
array.push(array[2]);

Open in new window

0
 
LVL 12

Assisted Solution

by:trinitrotoluene
trinitrotoluene earned 600 total points
ID: 40294572
objects are passed by reference so you should be able to push as many references as possible into the array.

modifying the actual object will also reflect in the array elements.

var obj1 ={id: rd, name: red, number: 2}
var obj2 ={id: bl, name: blue, number: 3}

myarr.push(obj1);
myarr.push(obj1); 
myarr.push(obj2);
myarr.push(obj2);

Open in new window

0
 

Author Closing Comment

by:Conrado ZAVALA
ID: 40294583
Thank you very much for the help!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 43

Expert Comment

by:Rob
ID: 40294953
Conradozavala,

You are aware that changing the id of say array[2], will also change array[3]?

http://jsfiddle.net/rjurd/a16x1j88/2/
0
 
LVL 43

Expert Comment

by:Rob
ID: 40294955
More to reinforce what trinitrotoluene has said about objects getting passed by reference and not value.
0
 

Author Comment

by:Conrado ZAVALA
ID: 40296950
Hi Rob,

I've noticed that already.

That's a really big problem, I want to copy the exact array element but after that I change the info of that element or index, but is changing the previous element too.

So is there a way to copy the element without having this problem?
to have different elements with different information?

Thank you very much for your help.

Best Regards.
0
 
LVL 83

Expert Comment

by:leakim971
ID: 40296959
var array = [];
array[0] = { id: "gr", name: "green", number: 1};
array.push({ id: "rd", name: "red", number: 2});
array[2] = { id: "bl", name: "blue", number: 3};
var clone = {};
for(var j in array[2]) {
    clone[j] = array[2][j];
}
array.push(clone);

Open in new window

0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question