I have two arrays of JSON objects. One with contractor details and another with projects. Both have a common field user_id.
I want a resultant array of objects such that each contractor also has an array of his active projects. Example:
arr1 = [{
name: 'Contractor A',
user_id: 3,
mobile_number: '9999999999',
active_projects: []
},
{
name: 'Contractor B',
user_id: 6,
mobile_number: '9999999999',
active_projects: []
}]
arr2 = [{
user_id: 3, project_name: 'Project A'
},
{
user_id: 3, project_name: 'Project B'
},
{
user_id: 6, project_name: 'Project C'
}]
The final array should be:
arr1 = [{
name: 'Contractor A',
user_id: 3,
mobile_number: '9999999999',
active_projects: ['Project A', 'Project B']
},
{
name: 'Contractor B',
user_id: 6,
mobile_number: '9999999999',
active_projects: ['Project C']
}]
What is the best/cleanest way to achieve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…