Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Reducir la lista de javascript de listas a un diccionario de listas

Similar a Lista de Listas a Diccionario de Listas

Me gustaría reducir una lista de listas a un diccionario de listas Por ejemplo

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

se convertiría

 dict = {'example': [55,66], 'example2': [44]}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Parece haber hecho algo práctico, es por eso que responde esto.

Array.reduce y la desestructuración de matrices lo ayudarán.

Consulte el comentario del código para conocer la lógica.

 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 Report

0

Aquí viene otra variante de cómo podría implementarse la función reductora. Intenta ser más legible y también presenta un comentario para cada uno de los 3 pasos que se deben realizar con cada ciclo de reducción.

Enlaces de documentación:

  • desestructuración de matriz

  • matriz.prototipo.reducir

  • Array.prototype.push

  • ??= El operador lógico de asignación nula

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!