简体   繁体   中英

Compare 2 array using lodash

I have 2 arrays of similar element but arranged in different order. I have a source variable with is arranged according to arr1 based on key src and file of arr1 and source. Now i want to arranged destination variable according to arr2 . Could you please let me know how we can done with lodash?

arr1 = [{x:0,y:1,src:a1},{x:1,y:1,src:b1},{x:2,y:1,src:c1}]

arr2 = [{x:1,y:1,src:b1},{x:1,y:1,src:a1},{x:1,y:1,src:c1}]

source = [{file:a1},{file:b1},{file:c1}]
Destination = [{file:b1},{file:a1},{file:c1}]

You could use lodash's map routine to achieve this:

let source = [{
  x: 1,
  y: 1,
  src: 'b1'
}, {
  x: 1,
  y: 1,
  src: 'a1'
}, {
  x: 1,
  y: 1,
  src: 'c1'
}]

let destination = _.map(source, value => {
    return { file: value['src']}
})

See here for an example .

Also note that you can achieve this using the standard JavaScript map function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM