【问题标题】:sort item with parent an child in typescript and angular在打字稿和角度中使用父母和孩子对项目进行排序
【发布时间】:2020-09-30 15:38:07
【问题描述】:

我有这个清单:

0: {id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4}

1: {id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null}

2: {id: 5, name: "111", code: "111", type: 3, hasParent: true, parentId: 4}

3: {id: 4, name: "22", code: "22", type: 1, hasParent: false, parentId: null}

4: {id: 3, name: "yyy", code: "yyyy", type: 2, hasParent: false, parentId: null}

5: {id: 2, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: true, parentId: 1}

6: {id: 1, name: "cbcvb", code: "cvbcvcbv", type: 2, hasParent: false, parentId: null}

我需要按 parent 和 child 对这个列表进行排序。

如果 item 的 parent 值等于另一个 item 的 id 值,则具有 parentId 值的 item 应该放在 parent 值等于 id 值的 item 下。

喜欢这个列表:

4: {id: 3, name: "yyy", code: "yyyy", type: 2, hasParent: false, parentId: 6}

1: {id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null}

0: {id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4}

2: {id: 5, name: "111", code: "111", type: 3, hasParent: true, parentId: 4}

3: {id: 4, name: "22", code: "22", type: 1, hasParent: false, parentId: null}

5: {id: 2, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: true, parentId: 1}

6: {id: 1, name: "cbcvb", code: "cvbcvcbv", type: 2, hasParent: false, parentId: null}

我写了这段代码,但它没有工作并且没有对项目列表进行排序:

    var Data = [{ id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4 },
{ id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null },
{ id: 5, name: "111", code: "111", type: 3, hasParent: true, parentId: 4 },
{ id: 4, name: "22", code: "22", type: 1, hasParent: false, parentId: null },
{ id: 3, name: "yyy", code: "yyyy", type: 2, hasParent: false, parentId: null },
{ id: 2, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: true, parentId: 1 },
{ id: 1, name: "cbcvb", code: "cvbcvcbv", type: 2, hasParent: false, parentId: null }];


var result = [];
Data.forEach((values) => {
    if (result.indexOf(values) === -1) {
        result.push(values);
    }
    if (values.parentId !== null) {
        var d_ = Data.filter(srch => {
            return values.parentId === srch.id;
        });
        if (result.indexOf(d_[0]) === -1) {
            result.push(d_[0]);
        }
    }
});
console.log(result);

什么问题?这个问题怎么解决???

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    如果它没有嵌套元素,我认为您可以做一些类似的事情(但不会给您相同的结果-真的我认为您有错误,parentId 3 不是 6?-)

    this.sorted=[];
    //get all the elements that has parent
    const childs=this.data.filter(x=>x.parentId).sort((a,b)=>a.parentId-b.parentId)
    
    //for each
    childs.forEach((x,i)=>{
      this.sorted.push(x)     //<--add to sorted
                              //if is the last or the next has diferent parent
      if (i==childs.length-1 || childs[i+1].parentId!=x.parentId)
         this.sorted.push(this.data.find(p=>p.id==x.parentId)) //<--add the parent
    
    })
    //finally include the elements that is not in the list
    this.data.forEach(x=>{
      if (!this.sorted.find(s=>s.id==x.id))
        this.sorted.push(x)
    })
    

    【讨论】:

    • 我只是更改了一些代码,因为它不需要检查 childs[i+1]。每一行的评论对你没有帮助吗?首先获取所有 parentIdnull 的元素。我将每个都添加到 this.sorted 中。但检查孩子列表中的下一个是否有不同的父母(或者是最后一个孩子)。如果发生这种情况,请添加元素。现在假设您有一个没有子元素的元素(没有项目的 ParentId 等于此 id)。这不在列表中,所以我们检查添加它
    猜你喜欢
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    相关资源
    最近更新 更多