我有一个对象obj1,里面有另一个对象obj2。obj2以相同的方式构建,obj1因此其中将有另一个对象。假设我有 20 个,我正试图进入其中的每一个以获取一些数据。有没有办法创建一个循环,一旦它看到它就进入 obj 内部?我尝试这样做但没有运气。var location;for (var [key, value] of Object.entries(object)) { var type = typeof value; var array = Array.isArray(value); if (typeof value === "object" && array === false && value) { location = key; for (var [a, b] of Object.entries(object[location])) { /*this is where I'm stuck, the location variable doesn't update with the 'path' of every object*/ } }}
1 回答

郎朗坤
TA贡献1921条经验 获得超9个赞
创建一个在一层循环的函数。检查对象的值。如果找到一个,请回忆起相同的功能。
function createLoop(obj) {
for (const key in obj) {
if (typeof obj[key] == "object") {
createLoop(obj[key])
} else {
// do your stuff
}
}
}
添加回答
举报
0/150
提交
取消