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

115
Views
concatenar propiedad de matriz anidada de un objeto

Tengo un archivo JSONLines que contiene miles de elementos y necesito fusionarlos todos, pero para las matrices en lugar de fusionarlos, necesito concatenarlos. No tengo el esquema del elemento, así que necesito hacer esto dinámicamente.

 {title: 'test', data: [1]} {title: 'test', data: [2]} {title: 'test1', data: [3]}

y la salida será algo como esto

 {title: 'test1', data: [1,2,3]}

No estoy seguro de cuál es la mejor forma de rendimiento que puedo hacer.

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

0

No tan complicado Puedes separar los pasos. Me gusta:

 const array = [ { title: 'test', data: [1], data2: ['data1', 'data2'] }, { title: 'test', data: [2], data2: [1, 2] }, { title: 'test1', data: [3], data2: [1, 2, 3] }, { title: 'test1', data2: [1, 2, 3] }, { title: 'test1', data2: 'string' }, { title: 'test3', data2: 69 }, ]; const mergeAllKeys = (newArr, val, ix, mergeOn) => { return Object.keys(newArr[ix]) .filter((k) => k !== mergeOn) .reduce((obj, k) => ({ ...obj, [k]: [ ...newArr[ix][k], ...(Array.isArray(val[k]) ? val[k] : [val[k]]), ].filter((v) => v), }), {}); }; const keysToArray = (val, mergeOn) => { return Object.keys(val) .filter((k) => k !== mergeOn) .reduce((o, k) => ({ ...o, [k]: Array.isArray(val[k]) ? val[k] : [val[k]] }), {}); }; function mergeBy(arr, mergeOn) { const memo = []; return arr.reduce((newArr, val) => { const ix = memo.findIndex((v) => v === val[mergeOn]); if (ix > -1) { newArr[ix] = { [mergeOn]: newArr[ix][mergeOn], ...mergeAllKeys(newArr, val, ix, mergeOn), }; return newArr; } memo.push(val[mergeOn]); return [ ...newArr, { [mergeOn]: val[mergeOn], ...keysToArray(val, mergeOn), }, ]; }, []);

Y luego ponerlo todo junto:

 const array = [ { title: 'test', data: [1], data2: ['data1', 'data2'] }, { title: 'test', data: [2], data2: [1, 2] }, { title: 'test1', data: [3], data2: [1, 2, 3] }, { title: 'test1', data2: [1, 2, 3] }, { title: 'test1', data2: 'string' }, { title: 'test3', data2: 69 }, ]; function mergeBy(arr, mergeOn) { const memo = []; return arr.reduce((newArr, val) => { const ix = memo.findIndex((v) => v === val[mergeOn]); if (ix > -1) { newArr[ix] = { [mergeOn]: newArr[ix][mergeOn], ...Object.keys(newArr[ix]) .filter((k) => k !== mergeOn) .reduce((obj, k) => ({ ...obj, [k]: [ ...newArr[ix][k], ...(Array.isArray(val[k]) ? val[k] : [val[k]]), ].filter((v) => v), }), {}), }; return newArr; } memo.push(val[mergeOn]); return [ ...newArr, { [mergeOn]: val[mergeOn], ...Object.keys(val) .filter((k) => k !== mergeOn) .reduce((o, k) => ({ ...o, [k]: Array.isArray(val[k]) ? val[k] : [val[k]] }), {}), }, ]; }, []); } const funkyMerge = mergeBy(array, 'title'); console.log('funkyMerge: ', funkyMerge);

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!