Is there a better way to iterate over two array of objects than what I have done below? It seems messy to do it this way. I'm using lodash.
var array1 = [
{id:4356, name: 'James', sex: 'male'},
{id:7899, name: 'Jimmy', sex: 'male'},
{id:2389, name: 'Dawn', sex: 'female'}
];
var array2 = [
{id:4356, salary: 1000, job: 'programmer'},
{id:7899, salary: 2000, job: 'tester'},
{id:2389, salary: 3000, job: 'manager'}
];
Example output:
console.log(array1[0])
{
id:4356,
name: James,
sex: male,
person: {
id:4356,
salary: 1000,
job: programmer
}
}
Function:
_.forEach(array1, function(item1) {
_.forEach(array2, function(item2) {
if(item1.id === item2.id){
item1.person = item2;
}
});
});
id -> personmap first?