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

225
Views
Intentando obtener el formato final correcto en una matriz de un mapa, para actualizar usando una API REST, pero no puedo JS

Estoy tratando de formatear algunos datos para actualizar por lotes usando la API REST de Woocommerce.

Formato de matriz de destino:

 update: [ { id: 1, app: 'string1string2string3' }, { id: 2, app: 'string2' }, { id: 3, app: 'string2' }, { id: 5, app: 'string2' } ]

Ejemplo reproducible

 arr1 = [{ id: 1, app: 'string1' }, { id: 1, app: 'string2' }, { id: 1, app: 'string3' }, { id: 2, app: 'string2' }, { id: 3, app: 'string2' }, { id: 5, app: 'string2' }]; let a = new Map(); arr1.forEach((e) => { if (a.get(e.id)) { a.get(e.id).app += e.app; } else { a.set(e.id, e) } }) const finalizado = Array.from(a) console.log(finalizado); var temporary, chunk = 100; for (let i = 0; i < finalizado.length; i += chunk) { temporary = finalizado.slice(i, i + chunk); var payloadUp = { update: temporary }; console.log(payloadUp); }

Este es un ejemplo reproducible, mi primer intento fue simplemente formar una matriz desde el mapa:

 const finalizado = Array.from(a)

Eso no funcionó, entonces traté de darle algún formato:

 const finalizado = Array.from(a, [key, value] => { return ([key]: value); }

Pero creo que estoy fuera de mi alcance, no puedo entender el formato.

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

0

Solución

Use reduce() y Object.values() para establecer una matriz de destino como desee:

 arr1 = [{ id: 1, app: 'string1' }, { id: 1, app: 'string2' }, { id: 1, app: 'string3' }, { id: 2, app: 'string2' }, { id: 3, app: 'string2' }, { id: 5, app: 'string2' }]; const arrayHashmap = arr1.reduce((obj, item) => { obj[item.id] ? obj[item.id].app = obj[item.id].app.concat(item.app) : (obj[item.id] = { ...item }); return obj; }, {}); const mergedArray = Object.values(arrayHashmap); console.log(mergedArray);

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!