JavaScript地图更新子阵列都与所有相同的值
我创建了一个函数,以将相同的数量放在安排中,然后映射到分配该月的值,对不起,写下时听起来很奇怪。
但是,每当我运行它的子阵列中,正在更新的子阵列都会在所有数据中显示相同的数据:
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);
}
这结果是:
[{id:'001',name:'first',arrofstuff:[{ofend:'2022-01-01',value:13.33},{ofere {ofere:'2022-02-01',value:9.99 }],检查:0}, {id:'002',name:'second',arrofstuff:[{ofend:'2022-01-01',value:13.33},{ofere:'2022-02-01',value:9.99}],检查:1}, {id:'003',名称:'第三',arrofstuff:[{ofend:'2022-01-01',value:13.33},{ofere:'2022-02-01',value:9.99}],检查:3}, {id:'004',名称:'第四',arrofstuff:[{ofend:'2022-01-01',value:13.33},{ofend:'2022-02-01',value:9.99}],检查:4}]
我希望它得到:
[{iD:'001',name:'first',arrofstuff:[{ofend:'2022-01-01',value:10},{ofend:'2022-02-01',value,value:10 }],检查:0}, {id:'002',name:'second',arrofstuff:[{ofend:'2022-01-01',value:11.11},{ofere:'2022-02-01',value:''},],检查:1}, {id:'003',name:'third',arrofstuff:[{strie:'2022-01-01',value:12.12},{ofere:'2022-02-01',value:9.99}],检查:3}, {id:'004',名称:'第四',arrofstuff:[{ofend:'2022-01-01',value:13.33},{ofient:'2022-02-01',value:'''}],检查:4}]
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}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用此代码解决了它:
我以前遇到了这个问题,它与内存分配有关,因此您必须将对象作为字符串将对象放入存储器中,然后解析回去以用作对象。
I solved it with this code:
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.