Say I have two arrays:
a = [
{ name: “Texas”,
pop: 15000},
{ name: “California”,
pop: 45000},
{ name: “Alaska”,
pop: 5000}
]
b = [
{ name: “Texas”,
deaths: 2000},
{ name: “California”,
deaths: 1000},
{ name: “Alaska”,
deaths: 500}
]
Like so and I am trying to add a deaths column to the “a” array based on the name being the key.
So the completed array would look like:
a = [
{ name: “Texas”,
pop: 15000,
deaths: 2000},
{ name: “California”,
pop: 45000
deaths: 1000},
{ name: “Alaska”,
pop: 5000,
deaths:500}
]
so I attempt:
a.map(d => ({
name: d.name,
pop: d.pop,
deaths: d.filter((b => b.name) === d.name)).deaths
})) //but this just throws a “not a function error”
and deaths: b.map(d => d.deaths) //this creates the column but maps the entire array to each object no
//filtering based on the name field…
Is there an easier way to accomplish this? Am I even on the right track? Any and all help is appreciated, I would like to learn how to manipulate the data inside of observable instead of just going into the datasheets and manually creating these columns…