1 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
你的代碼完美運(yùn)行。我懷疑這是你創(chuàng)建對(duì)象的方式。
在這里,我只是提供了一個(gè)id, 字符串 asname并發(fā)送到您的children對(duì)象中。您的代碼未經(jīng)修改即可創(chuàng)建您所期望的內(nèi)容:
class Item {
constructor(id, name, children = []) {
this.id = id;
this.name = name;
this.children = children.map((child) => {
return new Item(child.id, child.name, child.children)
});
}
test() {
console.log("Test");
}
//...
}
let children = [{
id: 1,
name: 'foo'
},
{
id: 2,
name: 'bar'
},
{
id: 3,
name: 'foobar',
children: [{
id: 4,
name: 'foo'
},
{
id: 5,
name: 'bar'
},
]
},
];
let newItem = new Item(1, 'item', children)
console.log(newItem)
添加回答
舉報(bào)