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

92
Visualizações
How to reduce array to object with sub-keys

I cannot seem to get my head wrapped around the JS array.reduce() function. I have the following input array and want to transform it into an object with reduce to get the following output:

Input:

let cars = [ 
{ name: 'bmw', type: 'diesel' },
{ name: 'mercedes', type: 'gas' },
{ name: 'honda', type: 'electric' },
{ name: 'bmw', type: 'gas' }
];

Desired output:

{ 
"bmw": [{type: 'diesel'}, {type: 'gas'}],
"mercedes": [{"type": "gas"}],
"honda": [{"type": "electric"}]
}

What I have tried:

let output = input.reduce((acc, item) => {
  if (!acc[item.name]) {
    acc[item.name] = [{ type: item.type }];
  } else {
    acc[item.name] = []; // How can I add the value of the second bmw occurrence into the array?
  }
  return acc;
}, {});

console.log(output);

What I have got:

{ 
"bmw": [],
"mercedes": [{"type": "gas"}],
"honda": [{"type": "electric"}]
}

Any pointers greatly appreciated as JS newbie here. Thanks!

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can spread existing array value and add new value

let output = cars.reduce((acc, item) => {
  if (!acc[item.name]) {
    acc[item.name] = [{ type: item.type }];
  } else {
    acc[item.name] = [...acc[item.name], { type: item.type }]; // spread and insert
  }
  return acc;
}, {});

Reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

about 4 years ago · Santiago Trujillo Relatório

0

This is basically the same logic as kwaiks solution. Main differences is this does not infer truthiness from whether an entry is defined but askes explicitly about the property and instead of spread this simply uses the push method.

let output = cars.reduce((acc, currentValue, currentIndex, arr) => { 
  acc.hasOwnProperty(currentValue.name)
  ? acc[currentValue.name].push({type: currentValue.type})
  : acc[currentValue.name] = [{type: currentValue.type}]; 
  return acc; 
}, {});

If you do not mean to redeclare output you may want to swap let with const.

about 4 years ago · Santiago Trujillo 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