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

257
Views
¿Cómo eliminar duplicados de una matriz de objetos y combinar valores de esos duplicados?

Tengo una matriz como esta, mi id y mi name serán los mismos para varios objetos, pero los valores de las organizations pueden ser diferentes

 array= [ {id: 1, name: "Test1", organizations: 1}, {id: 1, name: "Test1", organizations: 2}, {id: 1, name: "Test1", organizations: 3}, {id: 2, name: "Test2", organizations: 4}, {id: 2, name: "Test2", organizations: 5}, {id: 2, name: "Test2", organizations: 6} ];

Quiero convertir esto para que sea así:

 expectedArray = [ {id: 1, name: "Test1", organizations: [1,2,3]}, {id: 2, name: "Test2", organizations: [4,5,6]} ];

Puede ayudarme alguien, por favor

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

 const array= [ {id: 1, name: "Test1", organizations: 1}, {id: 1, name: "Test1", organizations: 2}, {id: 1, name: "Test1", organizations: 3}, {id: 2, name: "Test2", organizations: 4}, {id: 2, name: "Test2", organizations: 5}, {id: 2, name: "Test2", organizations: 6} ]; const mergeDuplicates= (field, uniqueField) => (source = [], value)=> { const target = source.find( item => item[uniqueField] == value[uniqueField] ); if(target) target[field].push( value[field] ); else source.push({...value, [field]: [ value[field] ] }); return source; } const mergeOrganaizationsById = mergeDuplicates('organizations','id') const result = array.reduce(mergeOrganaizationsById, []) console.log(result)

about 4 years ago · Santiago Trujillo Report

0

Querrás reducir.

 array.reduce((acc, cur) => { const i = acc.findIndex(a => a.id === cur.id && a.name === cur.name); if (i === -1) return [...acc, {...cur, organizations: [cur.organizations]}]; return [...acc.slice(0, i), {...cur, organizations: [...acc[i].organizations, cur.organizations]}, ...acc.slice(i +1)]; }, []);
about 4 years ago · Santiago Trujillo Report

0

Puede lograr la salida utilizando forEach agrupando según el name y luego insertando los campos necesarios en la matriz de salida.

 const array = [ { id: 1, name: "Test1", organizations: 1 }, { id: 1, name: "Test1", organizations: 2 }, { id: 1, name: "Test1", organizations: 3 }, { id: 2, name: "Test2", organizations: 4 }, { id: 2, name: "Test2", organizations: 5 }, { id: 2, name: "Test2", organizations: 6 } ]; const current = Object.create(null); const finalArr = []; array.forEach(function (o) { if (!current[o.name]) { current[o.name] = []; finalArr.push({ id: o.id, name: o.name, organizations: current[o.name] }); } current[o.name].push(o.organizations); }); console.log(finalArr);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Santiago Trujillo 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!