I am attempting to combine data from two http requests into a single object based on specific conditions. Take a look at the following objects:
vehicles: [
{
vId: 1,
color: 'green',
passengers: [
{
name: 'Joe',
age: 22
},
{
name: 'Jack',
age: 32
}
]
}
more vehicles....]
pDetails: [
{
vId: 1,
detail: tall
},
{
vId: 1,
detail: 'fat'
},
{
vId 2,
detail: 'sad'
} more details.....
]
If the vIds in both objects match, I want to merge pDetails into a new array within the corresponding vehicle object in the vehicles array. My project is built using Angular 5 and Typescript with Lodash integrated. I am struggling with iterating through nested arrays as I am still fairly new to JavaScript and not completely familiar with its APIs. Are there any features in TypeScript, ES6, Angular, or Lodash that could simplify this process for me?
Here is what I have managed to achieve so far:
this.getVehicles().subscribe(
data => { v = data.result.objects },
err => console.error(err),
() => Object.keys(v).forEach(key => {
console.log(v[key]); //mapping logic here
})
While this successfully loops through the vehicles array, my goal is to iterate through each passenger object within each vehicle and search for matches in the pDetails array, copy them, push them into a temporary array, and then assign them to the passengers array of the respective vehicle. I am still struggling to understand how to iterate through each vehicle's passenger array.