JavaScript地图更新子阵列都与所有相同的值

发布于 2025-01-30 19:52:36 字数 2041 浏览 5 评论 0原文

我创建了一个函数,以将相同的数量放在安排中,然后映射到分配该月的值,对不起,写下时听起来很奇怪。

但是,每当我运行它的子阵列中,正在更新的子阵列都会在所有数据中显示相同的数据:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎会甘心 2025-02-06 19:52:36

我用此代码解决了它:

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文