Get all keys of a deep object in Javascript

I have the following object:

var abc = {
    1: "Raggruppamento a 1",
    2: "Raggruppamento a 2",
    3: "Raggruppamento a 3",
    4: "Raggruppamento a 4",
    count: '3',
    counter: {
        count: '3',
    },
    5: {
        test: "Raggruppamento a 1",

        tester: {
            name: "Ross"
        }
    }
};

I would like to get the following result:

  • abc [1]
  • abc [2]
  • ABC [3]
  • abc [4]
  • abc.count
  • abc.counter.count
  • ABC [5]
  • abc [5] .test
  • abc [5] .tester
  • ABC [5] .tester.name

is it possible to use nodejs, possibly with plugins?

+13
source share
4 answers

You can do this by recursively moving the object:

function getDeepKeys(obj) {
    var keys = [];
    for(var key in obj) {
        keys.push(key);
        if(typeof obj[key] === "object") {
            var subkeys = getDeepKeys(obj[key]);
            keys = keys.concat(subkeys.map(function(subkey) {
                return key + "." + subkey;
            }));
        }
    }
    return keys;
}

Running the getDeepKeys(abc)object in your question will return the following array:

["1", "2", "3", "4", "5", "5.test", "5.tester", "5.tester.name", "count", "counter", "counter.count"]
+19
source

Smaller version, without side effect, just one line in the function body:

function objectDeepKeys(obj){
  return Object.keys(obj).filter(key => obj[key] instanceof Object).map(key => objectDeepKeys(obj[key]).map(k => `${key}.${k}`)).reduce((x, y) => x.concat(y), Object.keys(obj))
}

var abc = {
    1: "Raggruppamento a 1",
    2: "Raggruppamento a 2",
    3: "Raggruppamento a 3",
    4: "Raggruppamento a 4",
    count: '3',
    counter: {
        count: '3',
    },
    5: {
        test: "Raggruppamento a 1",

        tester: {
            name: "Ross"
        }
    }
};

function objectDeepKeys(obj){
  return Object.keys(obj)
    .filter(key => obj[key] instanceof Object)
    .map(key => objectDeepKeys(obj[key]).map(k => `${key}.${k}`))
    .reduce((x, y) => x.concat(y), Object.keys(obj))
}

console.log(objectDeepKeys(abc))
Run codeHide result
+3
source

, ...

This code covers all criteria in the format of a JSON object, such as just an object, an array of objects, a nested array of objects, a nested object with array objects, etc.

getDeepKeys = function (obj) {
  var keys = [];
    for(var key in obj) {
        if(typeof obj[key] === "object" && !Array.isArray(obj[key])) {
            var subkeys = getDeepKeys(obj[key]);
            keys = keys.concat(subkeys.map(function(subkey) {
                return key + "." + subkey;
            }));
        } else if(Array.isArray(obj[key])) {
            for(var i=0;i<obj[key].length;i++){
               var subkeys = getDeepKeys(obj[key][i]);
               keys = keys.concat(subkeys.map(function(subkey) {
                return key + "[" + i + "]" + "." + subkey;
               }));
            }
        } else {
          keys.push(key);
        }
    }
    return keys;
}
+1
source

I used this code (some correction for the previous code from Peter Olson, used lodash) to get the keys and check if some value is a value Date:

getDeepKeys = function (obj) {
  let keys = [];
  for (let key in Object.keys(obj)) {
    let value = obj[key];
    if (_.isDate(value)) {
      keys.push(key);
    } else if (_.isObject(value)) {
      let subkeys = getDeepKeys(value);
      keys = keys.concat(subkeys.map(function(subkey) {
        return key + "." + subkey;
      }));
    } else {
      keys.push(key)
    }
  }
  return keys;
}

I also checked if the value mongoDBRefis a condition used, for example:((_.isObject(value)) && (value && value.oid))

0
source

Source: https://habr.com/ru/post/1671817/


All Articles