Lodash v 4.17.15
Consider the scenario where two arrays are involved:
var users = [{
id: 12,
name: Adam
},{
id: 14,
name: Bob
},{
id: 16,
name: Charlie
},{
id: 18,
name: David
}
]
var jobs = [{
empid: 12,
profession: Engineer
},{
empid: 14,
profession: CEO
},{
empid: 16,
profession: CFO
},{
empid: 18,
profession: CTO
}
]
Desired Outcome
var jobsEmp= [{
empid: 12,
id: 12,
name: Adam
profession: Engineer
},{
empid: 14,
id: 14,
name: Bob
profession: CEO
},{
empid: 16,
id: 16,
name: Charlie
profession: CFO
},{
empid: 18,
id: 18,
name: David
profession: CTO
}
]
The typical approach is to :
var jobsEmp = _.forEach(users, function (emp) {
emp.jobs = _.find(jobs, function (jb) {
return jb.empid === emp.id;
});
});
However, this method may not be optimal. After reviewing the documentation extensively, it remains unclear on what technique is the most efficient and how to achieve this effectively using lodash.