JSFiddle - React, Tailwind, and code Playground
JavaScript
var data = {
"data": {
"location": {
"type": "List",
"count": 1,
"items": [{
"id": 1,
"type": "S",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY"
}
}, {
"id": 2,
"type": "S",
"address": {
"street": "1323 South St",
"city": "New York",
"state": "NY"
}
}]
}
}
}
//https://gist.github.com/megawac/6162481#file-underscore-lookup-js
var lookup = function (obj, key) {
var type = typeof key;
if (type == 'string' || type == "number") key = ("" + key).replace(/\[(.*?)\]/, function (m, key) { //handle case where [1] may occur
return '.' + key;
}).split('.');
for (var i = 0, l = key.length, currentkey; i < l; i++) {
if (obj.hasOwnProperty(key[i])) obj = obj[key[i]];
else return undefined;
}
return obj;
}
//syntax: lookup(jsonobj, input);
//Tests using your data
console.log(lookup(data, "data.location.type")) //=> "List"
console.log(lookup(data, "data.location.items[1].address.street")) //=> ""1323 South St"