简体   繁体   English

JavaScript 映射更新子数组为相同的值

[英]JavaScript map update subarray to all be the same value

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}] [{ 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 }],检查:1},{ id:'003',名称:'Third',arrOfStuff:[{期间:'2022-01-01',值:13.33},{期间:'2022-02-01 ', value: 9.99 }], check: 3}, { id: '004', name: 'Fourth', arrOfStuff: [{ period: '2022-01-01', value: 13.33 },{ period: '2022 -02-01',值:9.99 }],检查: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}] [{ id: '001', name: 'First', arrOfStuff: [{ period: '2022-01-01', value: 10 },{ period: '2022-02-01', value: 10 }],检查:0},{ id:'002',名称:'Second',arrOfStuff:[{期间:'2022-01-01',值:11.11},{期间:'2022-02-01',值: '' }],检查:1},{ id:'003',名称:'Third',arrOfStuff:[{期间:'2022-01-01',值:12.12},{期间:'2022-02- 01', value: 9.99 }], check: 3}, { id: '004', name: 'Fourth', arrOfStuff: [{ period: '2022-01-01', value: 13.33 },{ period: ' 2022-02-01',值:''}],检查: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.我以前遇到过这个问题,它与内存分配有关,因此您必须将对象作为字符串放入内存中,然后解析出来以用作对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM