I have created a function to put the same quantity in arrange and then to map over to allocated the value for that month, sorry sounds strange when written down.
But ever time I run it the subarray that is being updated displays the same data in all them:
this.calcTest([
{ id: '001', name: 'First', arrOfStuff: [{ period: '2022-01-01', value: 10 },{ period: '2022-02-01', value: 10 }]},
{ id: '002', name: 'Second', arrOfStuff: [{ period: '2022-01-01', value: 11.11 }]},
{ id: '003', name: 'Third', arrOfStuff: [{ period: '2022-01-01', value: 12.12 },{ period: '2022-02-01', value: 9.99 }]},
{ id: '004', name: 'Fourth', arrOfStuff: [{ period: '2022-01-01', value: 13.13 }] }]);
calcTest(d: any) {
const testData = [];
const periods = [
{ period: '2022-01-01', value: '' },
{ period: '2022-02-01', value: '' },
];
d.map((f, indexCheck) => {
let tempArr = f.arrOfStuff;
f.arrOfStuff = periods;
tempArr.map((t) => {
let i = f.arrOfStuff.findIndex((as) => as.period === t.period);
f.arrOfStuff[i].value = t.value;
});
f.check = indexCheck;
testData.push(f);
});
console.log(testData);
}
this results with:
[{ id: '001', name: 'First', arrOfStuff: [{ period: '2022-01-01', value: 13.33 },{ period: '2022-02-01', value: 9.99 }], check: 0}, { id: '002', name: 'Second', arrOfStuff: [{ period: '2022-01-01', value: 13.33 },{ period: '2022-02-01', value: 9.99 }], check: 1}, { id: '003', name: 'Third', arrOfStuff: [{ period: '2022-01-01', value: 13.33 },{ period: '2022-02-01', value: 9.99 }], check: 3}, { id: '004', name: 'Fourth', arrOfStuff: [{ period: '2022-01-01', value: 13.33 },{ period: '2022-02-01', value: 9.99 }], check: 4}]
I would like it to result in:
[{ id: '001', name: 'First', arrOfStuff: [{ period: '2022-01-01', value: 10 },{ period: '2022-02-01', value: 10 }], check: 0}, { id: '002', name: 'Second', arrOfStuff: [{ period: '2022-01-01', value: 11.11 },{ period: '2022-02-01', value: '' }], check: 1}, { id: '003', name: 'Third', arrOfStuff: [{ period: '2022-01-01', value: 12.12 },{ period: '2022-02-01', value: 9.99 }], check: 3}, { id: '004', name: 'Fourth', arrOfStuff: [{ period: '2022-01-01', value: 13.33 },{ period: '2022-02-01', value: '' }], check: 4}]
I solved it with this code:
calcTest(d: any) {
const periods = JSON.stringify([
{ period: '2022-01-01', value: '' },
{ period: '2022-02-01', value: '' },
]);
d.map((f, indexCheck) => {
let tempArr = f.arrOfStuff;
f.arrOfStuff = JSON.parse(periods);
tempArr.map((t) => {
let i = f.arrOfStuff.findIndex((as) => as.period === t.period);
f.arrOfStuff[i].value = t.value;
});
f.check = indexCheck;
});
console.log(d);
}
I have had this problem before and it has something to do with memory allocation so you have to put the object into memory as a string and then parse back out to use as an object.