javascript - Parsing JSON for related key/value within object -


this question has answer here:

say have json array containing bunch of different key/values per each object. know how use javascript target specific key/value, if want search through entire json file find item, , find related pair within same object.

for example, how scan following json "moby dick", find author tied title?

"store": {     "book": [        { "category": "reference",         "author": "nigel rees",         "title": "sayings of 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 rings",         "isbn": "0-395-19395-8",         "price": 22.99       }     ],   } } 

you'd have iterate list, can create custom functions, so:

function findtitle(title) {     (var = 0; < data.store.book.length; i++) {         if (data.store.book[i].title == title) {             return data.store.book[i];         }     } } 

so can do:

var book = findtitle("moby dick"),     author = book.author; 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -