Javascript 如何以智能方式映射错误数据
因此,我目前面临一个挑战,一个输出我无法控制的数据的API给了我以下数据结构(虚拟数据): 问题在于创建一个数据树结构,例如,我希望有这个结构(相同的虚拟数据,但经过重组): 是否有一些聪明的方法可以通过e6功能实现这一点,而不必在循环中创建可选循环 提前谢谢 编辑:我的工具是Javascript(es6)Javascript 如何以智能方式映射错误数据,javascript,ecmascript-6,Javascript,Ecmascript 6,因此,我目前面临一个挑战,一个输出我无法控制的数据的API给了我以下数据结构(虚拟数据): 问题在于创建一个数据树结构,例如,我希望有这个结构(相同的虚拟数据,但经过重组): 是否有一些聪明的方法可以通过e6功能实现这一点,而不必在循环中创建可选循环 提前谢谢 编辑:我的工具是Javascript(es6) [ { id: 1, text: 'some text' }, { id: 2, text: 'som
[
{
id: 1,
text: 'some text'
},
{
id: 2,
text: 'some text'
},
{
id: 3,
text: 'some text'
},{
id: 4,
text: 'some text'
},
{
id: 5,
text: 'some text',
parent_id: 1
},
{
id: 6,
text: 'some text',
parent_id: 5
}
]
[
{
id: 1,
text: 'some text'
nested: [
{
id: 5,
text: 'some text',
parent_id: 1
nested: [
{
id: 6,
text: 'some text',
parent_id: 5
}
]
}
],
},
{
id: 2,
text: 'some text'
},
{
id: 3,
text: 'some text'
},
{
id: 4,
text: 'some text'
}
]