Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中的父子关系排序_Javascript_Sorting_Tree - Fatal编程技术网

javascript中的父子关系排序

javascript中的父子关系排序,javascript,sorting,tree,Javascript,Sorting,Tree,我有以下结构 [ { "category_id" : 1, "parent_category" : null } , { "category_id" : 2, "parent_category" : 1 }, { "category_id" : 3, "parent_category" : 1 }, { "categ

我有以下结构

[
    {
         "category_id" : 1,
         "parent_category" : null
    } ,
    {
         "category_id" : 2,
         "parent_category" : 1
    },
    {
         "category_id" : 3,
         "parent_category" : 1
    },
    {
         "category_id" : 4,
         "parent_category" : 2
    },
    ,
    {
         "category_id" : 5,
         "parent_category" : null
    },
    ,
    {
         "category_id" : 6,
         "parent_category" : 5
    }


]
[
    {
      "parent_category":[ "array of all children that follow this main parent category" ]  
    },
    {},
    {}
]
所以我有一个父子关系,我想用下面的方法来排序 结构

[
    {
         "category_id" : 1,
         "parent_category" : null
    } ,
    {
         "category_id" : 2,
         "parent_category" : 1
    },
    {
         "category_id" : 3,
         "parent_category" : 1
    },
    {
         "category_id" : 4,
         "parent_category" : 2
    },
    ,
    {
         "category_id" : 5,
         "parent_category" : null
    },
    ,
    {
         "category_id" : 6,
         "parent_category" : 5
    }


]
[
    {
      "parent_category":[ "array of all children that follow this main parent category" ]  
    },
    {},
    {}
]
一、 我见过很多解决方案,但都是关于树结构输出的


谢谢

您可以尝试此解决方案

let json = [
    { id : 1, name : 'A', parent : 0 },
    { id : 2, name : 'B', parent : 0 },
    { id : 3, name : 'C', parent : 1 },
    { id : 4, name : 'D', parent : 2 },
    { id : 5, name : 'E', parent : 1 },
    { id : 6, name : 'F', parent : 2 },
    { id : 7, name : 'G', parent : 0 },
];

//collecting all parents (whose parent is 0)
let parents = json.filter(x => x.parent === 0);

//collecting all chlidren (whose parent is not 0)
let children = json.filter(x => x.parent !== 0);

//iterating through children and pushing it to **parents** json just below the parent
for(x of children){
    let index = parents.findIndex(obj => obj.id === x.parent);
    parents.splice(index+1, 0, x);
}
现在,父对象json按照父对象对对象进行排序

parents:
   [ { id: 1, name: 'A', parent: 0 },
     { id: 5, name: 'E', parent: 1 },
     { id: 3, name: 'C', parent: 1 },
     { id: 2, name: 'B', parent: 0 },
     { id: 6, name: 'F', parent: 2 },
     { id: 4, name: 'D', parent: 2 },
     { id: 7, name: 'G', parent: 0 } ]

希望这会有所帮助。

你的数组中有漏洞,@Andreas下划线与香草Javascript的可能副本。这不是duplicate@Tushar这是一个关于使用不同方法/解决方案(包括一些库,可能关注性能,但这不会影响a.Qua)对对象进行分组的问题。你能给出一个预期结果的真实示例吗?