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

73
Visualizações
Combing Variables with Same Id

I'm working on a project where i am trying to get array with all 50 US states. I have some data where the id is the abbreviated version of the state and some where the id is the name of the state. I want all of the data to be under an id that is equal to the name of the state.

For this, I have:

let Array9 = (orderLocationUS);
    
 Array9.forEach((state) => {
            if (state._id === 'NY' || state._id === 'New York' || state._id === 'ny' || state._id === 'N.Y.' ){
               state._id = 'New York'     
        }})
console.log(orderLocationUS)

console.log(orderLocationUS) gives:

[
  { _id: 'New York', count: 1, totalSales: 108.75 },
  { _id: 'New Jersey', count: 1, totalSales: 100 },
  { _id: 'New York', count: 31, totalSales: 627.93 }
]

I want the result:

[
  { _id: 'New Jersey', count: 1, totalSales: 100 },
  { _id: 'New York', count: 32, totalSales: 736.68 }
]

I tried to get the count by:

array10 = = orderLocationUS.filter((state) => state._id === 'New York').map(x => x.count).reduce((a, b) => a + b, 0)
console.log(array10)

This does give me 32, but I need this in an array format and I know this is probably not the right way to get the results I need. I would really appreciate suggestion on how to get the:

[
  { _id: 'New Jersey', count: 1, totalSales: 100 },
  { _id: 'New York', count: 32, totalSales: 736.68 }
]

array that I need.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use includes to check if existing then just add if already present.

function combine() {
  let array = [{
      _id: 'New York',
      count: 1,
      totalSales: 108.75
    },
    {
      _id: 'New Jersey',
      count: 1,
      totalSales: 100
    },
    {
      _id: 'New York',
      count: 31,
      totalSales: 627.93
    }
  ];
  let output = [];

  array.forEach(function(item) {
    let existing = output.filter(function(v, i) {
      return v._id == item._id;
    });
    if (existing.length) {
      let existingIndex = output.indexOf(existing[0]);
      output[existingIndex].totalSales += item.totalSales;
      output[existingIndex].count += item.count;
    } else
      output.push(item);
  });
  console.log(output);
}

combine();

output

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