Javascript 如何在比较键后将对象列表转换为嵌套对象
目标:根据嵌套对象的父值将对象列表放入嵌套对象中 下面是我的Javascript 如何在比较键后将对象列表转换为嵌套对象,javascript,jquery,angular,object,Javascript,Jquery,Angular,Object,目标:根据嵌套对象的父值将对象列表放入嵌套对象中 下面是我的json数据: "data": [ { "id": "coding-825x500", "source": { "information": { "fileid": "coding-825x500", "filesize": 67340 }, **"dependent": "d1bc270d"** }
json
数据:
"data": [
{
"id": "coding-825x500",
"source": {
"information": {
"fileid": "coding-825x500",
"filesize": 67340
},
**"dependent": "d1bc270d"**
}
},
{
"id": "d1bc270d",
"source": {
"information": {
"fileid": "d1bc270d",
"filesize": 193
},
"dependent": "parent"
}
},
{
"id": "1_iwPLQ",
"source": {
"information": {
"fileid": "1_iwPLQ",
"filesize": 580969
},
"dependent": "d1bc270d"
}
},
{
"id": "coding-825",
"source": {
"information": {
"fileid": "coding-825",
"filesize": 67340
},
"dependent": null
}
}
]
}
这里的每个对象都id&dependent
{
"id": A
"dependent":parent
},
{
"id": B
"dependent":A
},
{
"id": C
"dependent":A
},
{
"id": D
"dependent":null
}
如果id等于依赖id,则它应该是子项,如果依赖项是父项,则id==dependent
必须在此项下,如果dependent为null,则它也是没有子项的父项
下面我使用过滤器工作,但后来我不知道如何继续和创建嵌套对象
let info = this.dynamic.data.filter((val)=>{
console.log(val.id, ":::" ,val.source.dependent);
})
Stackblitz=>
所需输出:所有子对象都应位于父对象下,以便设置嵌套数据,该表可能类似于以下格式
{
"id": "A",
"dependent":parent
"nested":[
{
"id":"B",
"dependent":"A"
},
{
"id":"c",
"dependent":"A"
},
]
},
{
"id": "c",
"dependent":null
}
您可以构建一个树,并将其用于具有统一
null
值的'parent'
节点
这种方法也适用于未排序的数据
var data=[{id:'A',dependent:'parent'},{id:'B',dependent:'A'},{id:'D',dependent:null},{id:'C',dependent:'A'}],
树=函数(数据){
var t={};
data.forEach(o=>{
var parent=o.dependent=='parent'?null:o.dependent;
赋值(t[o.id]=t[o.id]|{},o);
t[parent]=t[parent]|{};
t[parent].nested=t[parent].nested | |[];
t[parent].nested.push(t[o.id]);
});
返回t.null.nested;
}(数据);
控制台日志(树)代码>
。作为控制台包装{max height:100%!important;top:0;}
您可以使用array reduce函数来实现这一点
var data=[{“id”:“coding-825x500”,“source”:{“information”:{“fileid”:“coding-25x500”,“filesize”:67340},“dependent”:“d1bc270d”{“id”:“d1bc270d”,“source”:{“information”:{“fileid”:“d1bc270d”,“filesize”:193},“dependent”:“parent”},{“id”:“1uiwplq”,“source”:{“information”:“1uiwplq”,“filesize”:580969},“dependent”:“d1bc270d”},{“id”:“coding-825”,“source”:{“information”:{“fileid”:“coding-825”,“filesize”:67340},“dependent”:null}];
var结果=数据减少(功能(acc、elem){
if(elem.source.dependent=='parent'){
var dependents=data.filter(函数(a){returna.source.dependent==elem.id});
if(dependents&&dependents.length){
elem.nested=elem.nested | |[];
(受抚养人| |[])。forEach(函数(d){
元素嵌套推送(d);
})
加速推力(elem);
}否则{
加速推力(elem);
}
}else if(elem.source.dependent==null){
加速推力(elem);
}
返回acc;
}, []);
console.log(结果);
.as控制台包装{最大高度:100%!重要;顶部:0;}
你能给出你想要的输出吗?那么,你想要的东西就很容易可视化了。@YogeshPatil更新了问题为什么第一个数据?为什么“parent”和null
?为什么不为所有数据使用唯一的标志,比如null
或undefined
?数据排序了吗?@NinaScholz如果我们提到dependent:parent,因为它有包含is id和other的对象为null bcoz它的id在任何其他依赖项中都不包含,但技术上没有区别,因为没有给出所有的父项。您将descedents与descedents混淆,但子项中有此信息。我做了相同的操作,但下面得到的错误是我的stackblitz属性“null”在typ上不存在依赖的
嵌套在source
中。请查看第一个数据集的第二种方法。我不明白。在您的第一种方法中,我们得到了这样的结果[{“id”:“a”,“dependent”:“parent”,“nested”:[{“id”:“B”,“dependent”:“a”},{“id”:“C,“dependent”:“A”}]},{“id”:“D”,“dependent”:null}]其中,在第二种方法中,我们得到了嵌套键firstright。如果子级在数据数组中位于父级之前,则可以执行此操作。如果希望获得相同的属性顺序,可以使用一个模板对象,将第一级的所有属性设置为null
。请参见编辑。