Copy an object of an array in the same array?

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.
Conrado ZAVALAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trinitrotolueneDirector - Software EngineeringCommented:
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
Conrado ZAVALAAuthor Commented:
Thank you very much for the help!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

RobOwner (Aidellio)Commented:
Conradozavala,

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

http://jsfiddle.net/rjurd/a16x1j88/2/
0
RobOwner (Aidellio)Commented:
More to reinforce what trinitrotoluene has said about objects getting passed by reference and not value.
0
Conrado ZAVALAAuthor Commented:
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
leakim971PluritechnicianCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.