我需要在内存中迭代javascript大对象,以便生成嵌套的json数据并插入到db中。对象结构如下:
var obj = {
"30": { "7": { "date1": { "key1": "value1", "key2": "value2"}}},
"29": { "8": { "date2": { "key1": "value1"}}},
"28": { "6": { "date3": { "key1": "value1", "key2": "value2", "key3": "value3"}}}
};
实际上我使用这个方法迭代对象以更改嵌套级别
Object.keys(obj).map((number1) => {
const number1Obj = obj[number1];
Object.keys(number1Obj).map((number2) => {
const number2Obj = number1Obj[number2];
Object.keys(number2Obj).map((date) => {
const dateObj = number2Obj[date];
console.log(number1);
console.log(number2);
console.log(date);
Object.keys(dateObj).map((key) => {
console.log(` ${key}:${dateObj[key]}`);
});
});
});
});
是否有更有效的方法迭代对象及其属性? running example