Search code examples
javascriptlodash

How to join 2 array object using lodash


I want to join a with b using lodash here is my data

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 ] } 
  ] 

Here is my output that I want

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

this is what I try I'm using 2 map data and inside the second map I use include check between 2 arrays..

_.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
    }
});

this method also work but is their any better method than using 2 map ?


Solution

  • Reduce b to a map of id to array of tests, and then map a, and take the relevant array from the map to create each object:

    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)