I have a JavaScript question that involves combining pieces of different arrays.
I have an array of objects below:
"people": [
{
"city": "SF",
"email": "[email protected]",
"firstName": "Bob",
"id": "1",
"lastName": "Smith"
},
{
"city": "Boston",
"email": "[email protected]",
"firstName": "Bill",
"id": "2",
"lastName": "Anderson"
},
{
"city": "Toronto",
"email": "[email protected]",
"firstName": "Ann",
"id": "3",
"lastName": "Kline",
}
]
I also have an array of tag arrays with an id object in each:
[["tag 1", "tag 2", {"id": "1"}], ["tag 8", "tag 2", {"id": "3"}]]
The expected output would be:
"people": [
{
"city": "SF",
"email": "[email protected]",
"firstName": "Bob",
"id": "1",
"lastName": "Smith",
"tags": ["tag 1", "tag 2"]
},
{
"city": "Boston",
"email": "[email protected]",
"firstName": "Bill",
"id": "2",
"lastName": "Anderson"
},
{
"city": "Toronto",
"email": "[email protected]",
"firstName": "Ann",
"id": "3",
"lastName": "Kline",
"tags": ["tag 8", "tag 2"]
}
]
Is there a way to add the tags to the objects above where the "id"'s match without doing a nested loop. I have tried a couple ways but have failed.
Array.prototype.map?