I have an array
listItems
0: "/static/media/m1.895f1b11.jpg"
1: "/static/media/m2.895f1b11.jpg"
length: 2
And a Object
item={
src: "",
key: ""
}
I`m trying to add each listItems to src in item like :
item={
src: "/static/media/m1.895f1b11.jpg",
key: 0
}
and this item into items :
//before :
items = [];
//after (this is what I want) :
items = [
{
src: "/static/media/m1.895f1b11.jpg",
key: 0
},
{
src: "/static/media/m2.895f1b11.jpg",
key: 1
}
];
to do this, I use the map in listItems and for each item I add it to src of my item object and then I add my item object to my array of objects items
listItems.map( (img,index) => {
item.src = img;
item.key = index;
console.log("item.src : " +item.src + " item.key : "+item.key + " img : "+img + " index "+ index);
items.push(item);
});
for(k=0;k<items2.length;k++)
console.log("items["+k+"] : "+items[k].src);
It looks fine, but when i get my log results :
item.src : /static/media/m1.895f1b11.jpg item.key : 0 img : /static/media/m1.895f1b11.jpg index 0
item.src : /static/media/m2.895f1b11.jpg item.key : 1 img : /static/media/m2.895f1b11.jpg index 1
items[0] : /static/media/m2.895f1b11.jpg
items[1] : /static/media/m2.895f1b11.jpg
Booth items[0] and items[1] are the same,where is the problem?