2

i have this list :

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}

and i need sort this list by parent and child .

If the parent value of the item is equal to the value of the id of another item, the item that has the value of the parentId should be placed under the item whose parent value is equal to id value.

like this list :

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}

i write this code but it not worked and not sorted the list of items :

    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);

whts the problem ? how can i solve this problem ????

1 Answer 1

3

If it's not nested the elements, I think you can do some like (but not give you the same result -really I think you has an error, parentId of 3 are not 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)
})
Sign up to request clarification or add additional context in comments.

1 Comment

I just change a few the code because it was unnnecesary check if childs[i+1]. The comment each line not help you?. First get all the elements that parentId<>null. And I added each to this.sorted. but check if the next in the list of childs has a different parent (or is the last child). If this happens add the elements. Now imagine you has an element with no children (no item has a ParentId equal this id). This is not in the list, so we check to add it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.