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

240
Visualizações
Reduce javascript list of lists to a dictionary of lists

Similar to List of Lists to Dictionary of Lists

Would like to reduce a list of lists to a dictionary of lists For example

list_list = [['example', 55], ['example', 66] , ['example2', 44]]

would become

dict = {'example': [55,66], 'example2': [44]}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Seems to have do some handson, thats why answering this.

Array.reduce and array destructuring will help you.

Please see the code comment for logic

const list_list = [['example', 55], ['example', 66] , ['example2', 44]];
//Destructuring the current value in the reduce function into [key, value]
const dict = list_list.reduce((acc, [key, value]) => {
  // If a node with the current key exist in the accumulator, merge the value of that node with current value
  // If node with current key doesnot exist, create a new node with that key and value as an array with current value being the element
  acc[key] = acc[key] ? [...acc[key], value] : [value];
  return acc;
}, {});
console.log(dict);

about 4 years ago · Juan Pablo Isaza Relatório

0

Here comes another variant of how the reducer function could be implemented. It tries to be more readable and also features a comment for each of the 3 steps which have to be taken with every reduce cycle.

Documentation links:

  • Array destructuring

  • Array.prototype.reduce

  • Array.prototype.push

  • ??= The logical nullish assignment operator

const list_list = [['example', 55], ['example', 66] , ['example2', 44]];

console.log(
  list_list

  //.reduce((result, item) => {
  //  // array destructuring of the currently processed array item.
  //  const [key, value] = item;

    // array destructuring within the reducer function's head.
    .reduce((result, [key, value]) => {

      // create and/or access the property list
      // identified by the array item's `key`.
      const groupList = (result[key] ??= []);

      // push the array item's `value`
      // into the above accessed list.
      groupList.push(value);

      // return the mutated `result` ... (the
      // stepwise aggregated final return value).
      return result;

    }, {}) // pass the final result's initial state as 2nd argument.
)

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