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

117
Views
Cómo unir 2 objetos de matriz usando lodash

Quiero unir a con b usando lodash aquí están mis datos

 const a = [ { id: 1, job: 'engineer' } , { id: 2, job: 'police' } , { id: 3, job: 'thief' } ] const b = [ { name: 'test1', score: 1, aId: [ 1, 2 ] } , { name: 'test2', score: 3, aId: [ 1, 3 ] } ]

Aquí está mi salida que quiero

 const result = [ { id: 1, job: 'engineer', score: [ {name: 'test1'}, {name: 'test2'} ] } , { id: 2, job: 'police', score: [] } , { id: 3, job: 'thief', score: [ {name: 'test 3'} ] } ]

esto es lo que intento. Estoy usando 2 datos de mapa y dentro del segundo mapa que uso, incluyo verificación entre 2 matrices.

 _.map(a, function(data:any) { const name = _.map(b, function(dataB:any) { // in here I check const isValid = dataB.aId.includes(a.id) if(isValid){ return { name = dataB.name } } }) return { id: data.id job: data.job score: name } });

este método también funciona, pero ¿hay algún método mejor que usar 2 mapas?

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

0

Reduzca b a un mapa de id a una matriz de pruebas, y luego asigne a , y tome la matriz relevante del mapa para crear cada objeto:

 const a = [ { id: 1, job: 'engineer' } , { id: 2, job: 'police' } , { id: 3, job: 'thief' } ] const b = [ { name: 'test1', score: 1, aId: [ 1, 2 ] } , { name: 'test2', score: 3, aId: [ 1, 3 ] } ] const bMap = b.reduce((acc, { name, aId }) => { const nameObject = { name } aId.forEach(id => { if(!acc.has(id)) acc.set(id, []) acc.get(id).push(nameObject) }) return acc }, new Map()) const result = a.map(o => ({ ...o, scores: bMap.get(o.id) })) console.log(result)

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!