Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda