Sunday, 13 August 2017

Unable store the objects into an array in javascript

I'm trying to store the objects into Javascript array.



var res=[
{name:'Alex',place:'US',age:20},
{name:'Jason',place:'Canada',age:25}

];

var obj={};
var data=[];

for ( let i in res){
obj.name=res[i].name;
obj.place=res[i].place;
data.push(obj);
}


console.log(data);


My expected output:



[ 
{name:'Alex',place:'US'}.
{name:'Jason',place:'Canada'}
]



Actual output I got:



[ 
{name:'Jason',place:'Canada'},
{name:'Jason',place:'Canada' }
]



Why I'm getting this type of output?
I'm noobie.Please help me.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...