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

139
Views
Agrupar por matriz de objetos en TypeScript sin ordenar

Estoy trabajando en una matriz de objetos para agruparlos según la clave (número), pero siempre termina ordenando la matriz. Entonces, ¿hay alguna solución que use Map() para realizar el grupo o cualquier solución de TypeScript para ello?

 const reGroup = (list, key) => { const newGroup = {}; list.forEach(item => { const newItem = Object.assign({}, item); newGroup[item[key]] = newGroup[item[key]] || []; newGroup[item[key]].push(newItem); }); return newGroup; }; const pets = [ {type:"Dog", age: "5", name:"Spot", }, {type:"Cat", age: "3", name:"Tiger",}, {type:"Dog", age: "2", name:"Rover", }, {type:"Cat", age: "3", name:"Leo", } ]; const grouped = reGroup(pets, "age"); console.log(grouped);

Producción:

 { "2": [ { "type": "Dog", "age": "2", "name": "Rover" } ], "3": [ { "type": "Cat", "age": "3", "name": "Tiger" }, { "type": "Cat", "age": "3", "name": "Leo" } ], "5": [ { "type": "Dog", "age": "5", "name": "Spot" } ] }

Esperado:

 { "5": [ { "type": "Dog", "age": "5", "name": "Spot" } ], "3": [ { "type": "Cat", "age": "3", "name": "Tiger" }, { "type": "Cat", "age": "3", "name": "Leo" } ], "2": [ { "type": "Dog", "age": "2", "name": "Rover" } ], }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Utilice una matriz en lugar de un objeto. No se puede garantizar el orden de las claves en los objetos.

Probar:

 const reGroup = (list, key) => { const groups = []; list.forEach(item => { let groupIndex = groups.findIndex((gi) => gi.key === item[key]); if (groupIndex === -1) { // when the group containing object does not exist in the array, // create it groups.push({key: item[key], items: []}); groupIndex = groups.length - 1; } const newItem = Object.assign({}, item); groups[groupIndex].items.push(newItem); }); return groups; }; const pets = [ {type:"Dog", age: "5", name:"Spot", }, {type:"Cat", age: "3", name:"Tiger",}, {type:"Dog", age: "2", name:"Rover", }, {type:"Cat", age: "3", name:"Leo", } ]; const grouped = reGroup(pets, "age"); console.log(JSON.stringify(grouped, null, 2));

que registra:

 [ { "key": "5", "items": [ { "type": "Dog", "age": "5", "name": "Spot" } ] }, { "key": "3", "items": [ { "type": "Cat", "age": "3", "name": "Tiger" }, { "type": "Cat", "age": "3", "name": "Leo" } ] }, { "key": "2", "items": [ { "type": "Dog", "age": "2", "name": "Rover" } ] } ]
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!