I have two array of objects and I want to merge them based on different properties and also concat if something doesnt exist in the list.
This is what I have:
const data1 = [{
name: 'A',
id: 1
}, {
name: 'B',
id: 2
}]
const data2 = [{
city: 'X',
rowID: 1
}, {
city: 'Y',
rowID: 2
}, {
city: 'Z',
rowID: 3
}]
const result = _.map(data1, function(p) {
return _.merge(
p,
_.find(data2, {
rowID: p.id
})
)
})
console.log(result)
//Expected Result
/**[
{
"name": "A",
"id": 1,
"city": "X",
"rowID": 1
},
{
"name": "B",
"id": 2,
"city": "Y",
"rowID": 2
},
{
"name": "",
"id": "",
"city": 'Z',
"rowID": 3
}
]*/
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
Please advice.