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

97
Views
Agrupe una matriz de objetos por ID de grupo

Tengo una variedad de objetos. Estos objetos deben agruparse según el ID de grupo. Además, es posible tener grupos anidados.

A continuación se muestra un ejemplo de la matriz y cómo quiero que se vea la matriz.

 [ { key: "1", label: "Random generated 0" }, { key: "2", groupId: "1", label: "Random generated 1" }, { key: "3", groupId: "1", label: "Random generated 2" }, { key: "4", groupId: "2", label: "Random generated 3" }, { key: "5", groupId: "2", label: "Random generated 4" }, { key: "6", label: "Random generated 5" }, { key: "7", label: "Random generated 6" } ];

Según la clave y el ID de grupo, debería aparecer una matriz anidada como este ejemplo

 [ { key: "1", label: "Random generated 0", children: [ { key: "2", groupId: "1", label: "Random generated 1", children: [ { key: "4", groupId: "2", label: "Random generated 3" }, { key: "5", groupId: "2", label: "Random generated 4" } ] }, { key: "3", groupId: "1", label: "Random generated 2" } ] }, { key: "6", label: "Random generated 5" }, { key: "7", label: "Random generated 6" } ];

¿Puedes ayudarme en la dirección correcta?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

puedes :

  1. crear una función recursiva que llene los niños de un elemento

     function fillChildren(elem) { var children = data.filter(oneData => oneData.groupId === elem.key); if (children.length) { elem.children = children; elem.children.map(fillChildren); } return elem; }
  2. combine array.filter y array.map para construir una estructura agrupada

     let grouped = data.filter(elem => !elem.groupId).map(fillChildren); 

 let data = [ { key: "1", label: "Random generated 0" }, { key: "2", groupId: "1", label: "Random generated 1" }, { key: "3", groupId: "1", label: "Random generated 2" }, { key: "4", groupId: "2", label: "Random generated 3" }, { key: "5", groupId: "2", label: "Random generated 4" }, { key: "6", label: "Random generated 5" }, { key: "7", label: "Random generated 6" } ]; function fillChildren(elem) { var children = data.filter(oneData => oneData.groupId === elem.key); if (children.length) { elem.children = children elem.children.map(fillChildren); } return elem; } let grouped = data.filter(elem => !elem.groupId).map(fillChildren); console.log(grouped)

about 4 years ago · Juan Pablo Isaza Report

0

Podría adoptar un enfoque estándar con un objeto para recopilar cada relación de hijo/padre y padre/hijo con un solo bucle.

La función getTree espera un conjunto de datos, el nombre de la propiedad para el id del objeto real, la propiedad principal, el nombre de los hijos deseados, si no son children y el valor raíz del padre, si no están undefined .

 const getTree = (data, id = 'id', parent = 'parent', children = 'children', root) => { const t = {}; data.forEach(o => ((t[o[parent]] ??= {})[children] ??= []).push(Object.assign(t[o[id]] ??= {}, o))); return t[root][children]; }, data = [{ key: "1", label: "Random generated 0" }, { key: "2", groupId: "1", label: "Random generated 1" }, { key: "3", groupId: "1", label: "Random generated 2" }, { key: "4", groupId: "2", label: "Random generated 3" }, { key: "5", groupId: "2", label: "Random generated 4" }, { key: "6", label: "Random generated 5" }, { key: "7", label: "Random generated 6" }], tree = getTree(data, 'key', 'groupId'); console.log(tree);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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!