I am trying to solve the following problem with LoDash. I know how to solve the problem using for loops, but am looking for the modern functional method of solving the problem.
I have the following data:
const data = {
people: [
{
name: "Bob",
vehicles: [
{
model: "Mazda"
tires: [
{ pressure: 20 },
{ pressure: 22 },
{ pressure: 23 },
{ pressure: 21 },
]
},
{
model: "Harley Davidson"
tires: [
{ pressure: 20 }
{ pressure: 25 }
]
}
]
},
{...},
{...},
{...},
]
}
From it, I want to extract a list of every tire pressure belonging to every person. So something like this:
[20, 22, 23, 21, 20, 25, ... ]
What I am looking for is a method that I can call like this:
const path = 'people.vehicles[*].tires[*].pressure';
const tirePressure = _.methodName(data, path);
I know that lodash has support for some similar functionality - like _.at(object, ['a[0].b.c', 'a[1]']);
(link), but it doesn't support collapsing the entire array, as far as I can tell.