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

221
Visualizações
Array concat is overwriting the values in for..each loop
const states = ["BR", "UP"];
const template = [
    {
      "State": "",
      "allotment": 25622
    },
    {
      "State": "",
      "expenditure": 1254
    }
  ];

let totalRecords: any[] = [];
states.forEach(state=>{
  const cc = template.map(record=> {record.State = state; return record})
  console.log(state, cc);// here i get the indiviual record set.
  totalRecords = totalRecords.concat(cc);
})
console.log(totalRecords);

here i am trying to get a concatenated list of template with different states, but somehow it is overwriting the previous records. although as seen in the console after adding map to template, a proper record set is seen. still it is not concatenating properly.

how to do it properly as required result is

[{
  "State": "BR",
  "allotment": 25622
}, {
  "State": "BR",
  "expenditure": 1254
}, {
  "State": "UP",
  "allotment": 25622
}, {
  "State": "UP",
  "expenditure": 1254
}] 
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The problem is that you're reusing the objects from template, just overwriting the properties of those objects. If you want to have multiple objects for each state, you'll need to create new objects instead:

const cc = template.map((record) => {
    record = { ...record, State: state }; // ***
    return record;
});

Live Example:

const states = ["BR", "UP"];
const template = [
    {
        State: "",
        allotment: 25622,
    },
    {
        State: "",
        expenditure: 1254,
    },
];

let totalRecords /*: any[]*/ = [];
states.forEach((state) => {
    const cc = template.map((record) => {
        record = { ...record, State: state };
        return record;
    });
    console.log(state, cc);
    totalRecords = totalRecords.concat(cc);
});
console.log(totalRecords);
.as-console-wrapper {
    max-height: 100% !important;
}

about 4 years ago · Juan Pablo Isaza Relatório

0

you can use flatMap and map for that like this

const states = ["BR", "UP"];
const template = [
    {
      "State": "",
      "allotment": 25622
    },
    {
      "State": "",
      "expenditure": 1254
    }
  ];


const totalRecords =  states.flatMap(State=> template.map(record=> ({...record, State})))

console.log(totalRecords)

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem in your program lies here: template.map(record=> {record.State = state; return record})

You are not returning a new object, you are overwriting a previous one.

Try this:

const states = ["BR", "UP"];
const template = [
    {
      "State": "",
      "allotment": 25622
    },
    {
      "State": "",
      "expenditure": 1254
    }
  ];

let totalRecords: any[] = [];
states.forEach(state=>{
  const cc = template.map(record=> ({...record, 'State': state}))
  totalRecords = totalRecords.concat(cc);
})
console.log(totalRecords);
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