I have an array as given below -
var x = [{
name: "Mr.X",
age: 22
},{
name: "Mr.Y",
age: 26
},{
name: "Mr.Z",
age: 24
},];
I want to duplicate the 2nd item and put it as the first element. Just to avoid any confusion, I want the resultant array to be as given below -
var x = [{
name: "Mr.YD",
age: 19
},{
name: "Mr.X",
age: 22
},{
name: "Mr.Y",
age: 26
},{
name: "Mr.Z",
age: 24
},];
What I have tried and got - I extracted the 2nd item, and changed the name and age properties. But it is also changing the 2nd item. I know it is because we are changing the reference. But i have no idea how to extract/duplicate item and change only its value.
JSON.stringifyis being used just to demonstrate the result..You can remove it!