Answer the question
In order to leave comments, you need to log in
How to recursively traverse a tree object?
Let's say we have a tree structure object:
const obj = {
something: {
something1: 'value',
something2: 'value1',
something3: {
something4: 'value2'
}
}
};
Answer the question
In order to leave comments, you need to log in
const obj = {
something: {
something1: 'value',
something2: 'value1',
something3: {
something4: 'value2'
}
}
}
const getKeys = obj => {
const res = []
for (const key in obj) {
res.push(key)
obj[key] && Object.getPrototypeOf(obj[key]) === Object.prototype && res.push(...getKeys(obj[key]))
}
return res
}
console.log(getKeys(obj))
//[ 'something', 'something1', 'something2', 'something3', 'something4' ]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question