let object = {
value1: {
car1: {
model: {}
},
car2: {
model: {}
}
},
value2: {
car1: {
model: {}
}
}
}
let result = ['value1', 'value2', 'car1', 'model', 'car2']
function objectKeys(o) {
return [...new Set([
Object.keys(o),
...Object.values(o).map(objectKeys)
].flat())]
}