One-liner javascript to collect values from object graph?
- by Kevin Pauli
Given the following object graph:
{
"children": [
{
"child": {
"pets": [
{
"pet": {
"name": "fido"
}
},
{
"pet": {
"name": "fluffy"
}
}
]
}
},
{
"child": {
"pets": [
{
"pet": {
"name": "spike"
}
}
]
}
}
]
}
What would be a nice one-liner (or two) to collect the names of my grandchildren's pets? The result should be ["fido", "fluffy", "spike"]
I don't want to write custom methods for this... I'm looking for something like the way jQuery works in selecting dom nodes, where you can just give it a CSS-like path and it collects them up for you.
I would expect the expression path to look something like "children child pets pet name"