So let’s assume I have a JSON file as such:
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
]
}
}
What I want to do is grab a member of “book” and all of its string value pairs where title is “Sword of Honour”. How can I do that with JQuery? The problem is that I don’t know the index ID of the the member, if I did it would be trivial, i.e. store.book[1].XXX would give me the value of all of the sibling key value pairs I am looking for.
Any ideas on how I can do this with minimum code?
Using array
filter:The result will be an array of all of the
bookelements that match the filter predicate.NB: this is an ES5 method. There’s a shim on the linked MDN page for non-ES5 browsers.