My code is so far
var chartSeriesArray = [{"EnableAnimation": true,"AnimationDuration": 1}];
let refArray = chartSeriesArray;
let clonedArray = [...chartSeriesArray]; // will clone the array
var x = [];
for(i=0;i<2;i++){
x.push(clonedArray);
}
x[0].foo = "bar";
console.log(x);
Console Output is
0:[{…}, foo: "bar"]
1:[{…}, foo: "bar"]
Whether I am trying to loop refArray
or clonedArray
in both the cases, it's added foo in both item 0
and 1
, i want to add only in 0
for example.
Expected output is
0:[{…}, foo: "bar"]
1:[{…}]
I want to access 0
and 1
individually later.
I tried everything but nothing works, Any help is highly appreciated.
Further after all suggestions, when i am trying below code
var metadata =
{
"KPISDetail": [{
"ChartSeriesList": {
"EnableAnimation": true,
"AnimationDuration": 1
}
}, {
"ChartSeriesList": {
"EnableAnimation": true,
"AnimationDuration": 1
}
}]
}
var data = [];
var x = [];
for(var l=0;l<2;l++){
data.push(metadata.KPISDetail[l].ChartSeriesList);
x.push(...data.map(o => Object.assign({}, o)))
}
x[0].foo = "bar";
x[1].foo = "foo";
console.log(x);
Result should be 2 only because my loop is executing 2 times. But i am getting output 3 times, which is wrong. I am getting below output