Last active
August 29, 2015 14:14
-
-
Save hbi99/a035c71cf6e34f47f9f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj = { | |
"car": [ | |
{"id": 10, "color": "silver", "name": "Volvo"}, | |
{"id": 11, "color": "red", "name": "Saab"}, | |
{"id": 12, "color": "red", "name": "Peugeot"}, | |
{"id": 13, "color": "yellow", "name": "Porsche"} | |
], | |
"bike": [ | |
{"id": 20, "color": "black", "name": "Cannondale"}, | |
{"id": 21, "color": "red", "name": "Shimano"} | |
] | |
}, | |
search = JSON.search(obj, '//car[color="yellow"]/name'); | |
console.log( search ); | |
// ["Porsche"] | |
var reds = JSON.search(obj, '//*[color="red"]'); | |
for (var i=0; i<reds.length; i++) { | |
console.log( reds[i].name ); | |
} | |
// Saab | |
// Peugeot | |
// Shimano | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment