Last active
July 5, 2021 21:57
-
-
Save vikneshwar/b4066a9f44bf3a29df9ab0ee7f4b66c7 to your computer and use it in GitHub Desktop.
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
| let val = { | |
| a: { b: { c: 10 }, d: { f: 20 } }, | |
| j: { l: { c: 30 } }, | |
| k: { k: { c: 40, k: 44 }, h: { f: 10 } }, | |
| }; | |
| // expected output arr = [20,10] // all the inner most f | |
| let arr = []; | |
| function getKeyValues(obj) { | |
| for (key in obj) { | |
| console.log(typeof key); | |
| if (typeof obj[key] === "object") { | |
| getKeyValues(obj[key]); | |
| } else if (typeof obj[key] !== "object") { | |
| arr.push(obj[key]); | |
| } | |
| } | |
| } | |
| //[10, 20, 30, "k"] | |
| console.log(arr); | |
| arr = []; | |
| // to get the f value | |
| function getSpecificValues(obj) { | |
| for (key in obj) { | |
| if (typeof obj[key] === "object") { | |
| if (obj[key].hasOwnProperty("f")) { | |
| console.log(obj[key].f); | |
| } else { | |
| getSpecificValues(obj[key]); | |
| } | |
| } | |
| } | |
| } | |
| getKeyValues(val); | |
| console.log(arr); | |
| const findValue = (obj, toFind) => { | |
| // console.log(typeof obj); | |
| if (typeof obj === "object") { | |
| for (key in obj) { | |
| // console.log(key) | |
| if (key === toFind && typeof obj[key] !== "object") { | |
| console.log(obj[key]); | |
| } else { | |
| findValue(obj[key], toFind); | |
| } | |
| } | |
| } | |
| }; | |
| findValue(val, "c"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment