Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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}]

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}]

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!