Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
Cree una matriz anidada de objetos y un grupo basado en 2 columnas en javascript

Tengo una variedad de objetos como a continuación.

 var array1 = [ { col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'}, { col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'}, { col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'}, { col1: 'RED4+R3', col2: 'RED', col3: 'D20'}, { col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}]; var result = array1.reduce(function(r, a) { r[a.col2] = r[a.col2] || []; r[a.col2].push(a); return r; }, Object.create(null)); console.log(result);

Intenté el script anterior, pero no pude lograrlo. en algún lugar me equivoqué.

Quiero que la salida como la siguiente necesite agrupar col1 y col3 en función de col2. Gracias por adelantado.

 var res = [ { "col1": [{text1: "ABC1+R2"},{text1: "ABC1+R"}], "col2": "ABC", "col3": [{text2: "R20"},{text2: "R301"}], }, { "col1": [{text1: "CDE2+R4"},{text1: "CDE2+R5"}], "col2": "CDE", "col3": [{text2: "R20"},{text2: "R30"}], }, { "col1": "RED4+R5", "col2": "RED", "col3": "D20" }, { "col1": "GRT3", "col2": "ED", "col3": "R20" } ]; console.log(res);

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

prueba este

 var array1 = [ { col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'}, { col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'}, { col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'}, { col1: 'RED4+R3', col2: 'RED', col3: 'D20'}, { col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}]; var result = array1.reduce(function(r, a) { let obj = r.find(el => el.col2 === a.col2); const text1 = {"text1": a.col1}; const text2 = {"text2": a.col3}; if(obj) { if(!obj.col1.find(el => el.text1 === text1.text1)) { obj.col1.push(text1); } if(!obj.col3.find(el => el.text2 === text2.text2)) { obj.col3.push(text2); } } else { obj = { col1: [text1], col2: a.col2, col3: [text2] } r.push(obj) } return r; }, []); console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Puede verificar el objeto si existe la clave, luego agregar un solo objeto; de lo contrario, verifique si una de las propiedades no es una matriz como valor, luego convierta el valor simple en un objeto dentro de una matriz.

 const data = [{ col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'}, { col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'}, { col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'}, { col1: 'RED4+R3', col2: 'RED', col3: 'D20'}, { col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}], result = Object.values(data.reduce(function(r, { col1, col2, col3 }) { if (r[col2]) { if (!Array.isArray(r[col2].col1)) { r[col2].col1 = [{ text1: r[col2].col1 }]; r[col2].col3 = [{ text2: r[col2].col3 }]; } r[col2].col1.push({ text1: col1 }); r[col2].col3.push({ text2: col3 }); } else { r[col2] = { col1, col2, col3 }; } return r; }, Object.create(null))); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

var array1 = [ { col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'}, { col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'}, { col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'}, { col1: 'RED4+R3', col2: 'RED', col3: 'D20'}, { col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}]; var result = array1.reduce(function(r, a) { r[a.col2] = r[a.col2] || []; r[a.col2].push(a); return r; }, Object.create(null)); let arr = [] for(let k in result){ let col1 = result[k].length > 1 ? result[k].map(el => ({text: el.col1})) : result[k][0]['col1']; let col2 = k; let col3 = result[k].length > 1 ? result[k].map(el => ({text: el.col3})) : result[k][0]['col1']; arr.push({col1, col2, col3}) } console.log(arr);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda