function flat(obj) {
let arr = [];
Object.entries(obj).forEach(([key, value]) => {
if (value.show) {
arr.push(key);
if (value.child) {
arr = arr.concat(flat(value.child));
}
}
})
return arr;
}
const arr = flat(obj);
function parseObject(object, name=undefined, result=[]) {
for (let key in object) {
if (object.hasOwnProperty(key)) {
if (key === 'show' && object['show']) {
result.push(name);
}
if (Object.prototype.toString.call(object[key]) === '[object Object]') {
parseObject(object[key], name ? `${name}.${key}` : `${key}`, result);
}
}
}
return result;
}
console.log(parseObject(obj)); // [ 'obj1', 'obj2', 'obj2.child.child2', 'obj3' ]