const obj = {
something: {
something1: 'value',
something2: 'value1',
something3: {
something4: 'value2'
}
}
};
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' ]
const getKeys = obj =>
obj instanceof Object
? Object.entries(obj).flatMap(n => [ n[0], ...getKeys(n[1]) ])
: [];
const keys = getKeys(obj);
function* getKeys(obj) {
if (Object(obj) === obj) {
for (const k in obj) if (obj.hasOwnProperty(k)) {
yield k;
yield* getKeys(obj[k]);
}
}
}
const keys = Array.from(getKeys(obj));
// или
for (const n of getKeys(obj)) {
console.log(n);
}