Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  164] [ 1]  / answers: 1 / hits: 16545  / 7 Years ago, wed, march 8, 2017, 12:00:00

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: Georgi
}
}
};


I would like to retrieve 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 that possible using nodejs maybe with the help of plugins?


More From » node.js

 Answers
24

You can do this by recursively traversing 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 getDeepKeys(abc) on the 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]

[#58628] Monday, March 6, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marcelofrankiea

Total Points: 200
Total Questions: 96
Total Answers: 101

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;