var obj = {
a: "12",
b: "23",
first: {
c: "34",
d: "45",
zou: null,
second: { 3: "56", f: "67", three: { g: "78", h: "89", i: "90" } },
},
};
function getObjAllKeys(obj, res = []) {
if (typeof obj !== 'object' || obj === null) return res;
// 使用 Object.keys() 获取对象自身的可枚举属性,包括原型链上的属性
const keys = Object.keys(obj);
keys.forEach((key) => {
res.push(key);
if (obj[key] && typeof obj[key] === 'object') {
getObjAllKeys(obj[key], res);
}
});
return res;
}
const result = getObjAllKeys(obj);
console.log(result);
// ['a', 'b', 'first', 'c', 'd', 'zou', 'second', '3', 'f', 'three', 'g', 'h', 'i']