Skip to content

Instantly share code, notes, and snippets.

@vikneshwar
Last active July 5, 2021 21:57
Show Gist options
  • Save vikneshwar/b4066a9f44bf3a29df9ab0ee7f4b66c7 to your computer and use it in GitHub Desktop.
Save vikneshwar/b4066a9f44bf3a29df9ab0ee7f4b66c7 to your computer and use it in GitHub Desktop.

Revisions

  1. vikneshwar revised this gist Jul 5, 2021. No changes.
  2. vikneshwar revised this gist Jul 5, 2021. 1 changed file with 40 additions and 25 deletions.
    65 changes: 40 additions & 25 deletions test.js
    Original 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}}
    }
    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 = []
    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])
    }
    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 = []
    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])
    }
    }
    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)
    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");
  3. vikneshwar created this gist Mar 12, 2019.
    42 changes: 42 additions & 0 deletions test.js
    Original 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)