Created
October 15, 2018 06:20
-
-
Save prameshbhattarai/9c2bf45507259ca06bb7b93ebaeac23c to your computer and use it in GitHub Desktop.
search object in JSON object in javascript
This file contains hidden or 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
| (function() { | |
| "use strict"; | |
| function searchByKeyPath(object, keys) { | |
| var deep = keys.split("."), obj = null; | |
| for(var i = 0; i < deep.length; i++) { | |
| (obj == null) ? obj = '' + deep[i] : obj = obj + '.' + deep[i]; | |
| if((eval(obj) === null || eval(obj) === undefined) && (i !== deep.length-1)) { | |
| var beforeNotFoundFrom = obj.substring(0, obj.lastIndexOf('.')); | |
| throw "Object not found from " + obj; | |
| } | |
| } | |
| return eval(obj); | |
| } | |
| function searchByKeys(object, keys) { | |
| var deep = keys.split("."); | |
| if(deep.length == 1 || !object[deep[0]]) return object[deep[0]]; | |
| var innerObject = object[deep[0]]; | |
| deep.shift(); | |
| return searchByKeys(innerObject, deep.join(".")); | |
| } | |
| function assert(condition, message) { | |
| if(!condition) { | |
| throw Error("Assertion failed" + (typeof message !== "undefined" ? ": " + message : "")); | |
| } else { | |
| console.log("Assertion Passed" + (typeof message !== "undefined" ? ": " + message : "")); | |
| } | |
| } | |
| function foundObjectTest(obj, keys) { | |
| "use strict"; | |
| var pharmacyList = obj; | |
| try { | |
| // deep object search | |
| var address = searchByKeys(pharmacyList, keys); | |
| assert(address === "address2String", "Expected address2 to be 'address2String'"); | |
| } catch(error) { | |
| // nothing to catch | |
| } | |
| } | |
| function notFoundObjectTest(obj, keys) { | |
| "use strict"; | |
| // should throw error | |
| // As our search is to find value inside address | |
| // pharmacyList[0] => pharmacy => address => address1 | |
| // But we are assigning address as null, so the search will throw error as "Object not found from" | |
| var pharmacyList = obj; | |
| try { | |
| pharmacyList[0].pharmacy.address = null; | |
| var address = searchByKeys(pharmacyList, keys); | |
| // following assertion will not be executed as we have throw error already. | |
| assert(address === "address1String", "Expected address1 to be 'address1String'"); | |
| } catch(error) { | |
| console.log("-----------------------------------"); | |
| console.log(error); | |
| console.log("-----------------------------------"); | |
| } | |
| } | |
| function assertionFailTest(obj, keys) { | |
| "use strict"; | |
| // should throw error | |
| // as our assertion will be failed | |
| var pharmacyList = obj; | |
| try { | |
| var address = searchByKeys(pharmacyList, keys); | |
| // following assertion will be executed. | |
| assert(address === "address2String", "Expected address1 to be 'address1String'"); | |
| } catch(error) { | |
| console.log("-----------------------------------"); | |
| console.log(error); | |
| console.log("-----------------------------------"); | |
| } | |
| } | |
| function main() { | |
| const pharmacyList = [ | |
| { | |
| pharmacy: { | |
| pharmacyName: "pharmacyNameString", | |
| pharmacyPhoneNumber: "pharmacyPhoneNumberString", | |
| address: { | |
| address1: "address1String", | |
| address2: "address2String", | |
| city: "cityString", | |
| state: "stateString", | |
| zipCode: "zipCodeString" | |
| } | |
| } | |
| } | |
| ]; | |
| // test for found object | |
| foundObjectTest(pharmacyList[0], "pharmacy.address.address2"); | |
| // foundObjectTest(pharmacyList, "object[0].pharmacy.address.address2"); // for search by key path | |
| // test for not found object | |
| // notFoundObjectTest(pharmacyList, "pharmacy.address.address1"); | |
| // notFoundObjectTest(pharmacyList, "object[0].pharmacy.address.address1"); // for search by key path | |
| // test for assert fail | |
| // assertionFailTest(pharmacyList[0], "pharmacy.address.address1"); | |
| // assertionFailTest(pharmacyList, "object[0].pharmacy.address.address1"); // for search by key path | |
| } | |
| // run our main function | |
| main(); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment