One-liner javascript to collect values from object graph?
Posted
by Kevin Pauli
on Stack Overflow
See other posts from Stack Overflow
or by Kevin Pauli
Published on 2010-04-30T21:36:10Z
Indexed on
2010/04/30
21:37 UTC
Read the original article
Hit count: 227
JavaScript
|JSON
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"
© Stack Overflow or respective owner