I have a structure like this:
let MainItem = [
{
"Id": "1",
"Cost": "1000"
},
{
"Id": "2",
"Cost": "5000"
},
{
"Id": "3",
"Cost": "2000"
},
{
"Id": "4",
"Cost": "3000"
}
];
I am going to change the value of Cost
each of the elements with map()
loop and store it in NewMainItem
.
let NewMainItem = MainItem.map((item, i) => {
item.cost = item.cost + 1000
return item
});
console.log(MainItem)
console.log(NewMainItem)
The main problem is that by changing Cost
in NewMainItem
, the value of
Cost
in MainItem
will be changed too, but I don not want to do this. By using map()
loop why the main object (MainItem
) will be changed too?