Last active
July 5, 2021 21:57
-
-
Save vikneshwar/b4066a9f44bf3a29df9ab0ee7f4b66c7 to your computer and use it in GitHub Desktop.
Revisions
-
vikneshwar revised this gist
Jul 5, 2021 . No changes.There are no files selected for viewing
-
vikneshwar revised this gist
Jul 5, 2021 . 1 changed file with 40 additions and 25 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,42 +1,57 @@ 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"); -
vikneshwar created this gist
Mar 12, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ 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)