我处理了嵌套对象,我想将其转换为递归数组或嵌套数组。我尝试像下面这样迭代对象,但它正在创建数组的单个对象。任何人都可以提出建议或分享您的想法,这对我有帮助 iterate(obj) { for (let property in obj) { this.configArray.push({key: property,children: [], isValue: false, value: ''}); if (obj.hasOwnProperty(property)) { const index = Object.keys(obj).indexOf(property); if (typeof obj[property] == "object") { this.iterate(obj[property]); } else { this.configArray[index].children.push({ key: property, value: obj[property], isValue: true, children: [] }); } } } }输入{"Parent 1": { "11": "Child 1", "12": "Child 2",},"Parent 2": { "20": { "21": "Grand Child 1", "22": "Grand Child 2", }},"Parent 3": { "31": "Child 1", "32": "Child 2",}}输出 [{ key: "Parent 1", value: "", children: [ { key: 11, value: "Child 1", children: [] }, { key: 12, value: "Child 2", children: [] } ]},{ key: "Parent 2", value: "", children: [ { key: 20, value: "", children: [ { key: 21, value: "Grand Child 1", children: [] }, { key: 22, value: "Grand Child 2", children: [] } ] } ]},{ key: "Parent 3", value: "", children: [ { key: 31, value: "Child 1", children: [] }, { key: 32, value: "Child 2", children: [] } ]},];
添加回答
举报
0/150
提交
取消