skip to Main Content

I have an array with objects:

[
    {
      id:1,
      parent:null,
      value1:22,
      value2:33
    },
    {
      id:2,
      parent:1,
      value1:0,
      value2:0
    },
    {
      id:3,
      parent:1,
      value1:40,
      value2:70
    },
    {
      id:4,
      parent:2,
      value1:40,
      value2:70
    },
    {
      id:5,
      parent:null,
      value1:40,
      value2:70
    }
]

I want to filter all the elements from array whose parent is present in same array in form of id.
Eg. id 1 and 5 should not present since they have null parent and null id is not there in array.

I tried –

array.filter( (item: IItem)=>{
   item.parent == item.id
});

This isnt filtering anything. Getting empty output.
I expect output to be all elements except 1 and 5.

5

Answers


  1. filter() with a nested find() should be enough:

    const data = [{id:1, parent:null, value1:22, value2:33 }, {id:2, parent:1, value1:0, value2:0 }, {id:3, parent:1, value1:40, value2:70 }, {id:4, parent:2, value1:40, value2:70 }, {id:5, parent:null, value1:40, value2:70 }];
    
    const res = data.filter(d => d.parent !== null && data.find(e => e.id === d.id));
    console.log(res);
    [
      {
        "id": 2,
        "parent": 1,
        "value1": 0,
        "value2": 0
      },
      {
        "id": 3,
        "parent": 1,
        "value1": 40,
        "value2": 70
      },
      {
        "id": 4,
        "parent": 2,
        "value1": 40,
        "value2": 70
      }
    ]
    
    Login or Signup to reply.
  2. Try this. If parent is false (null) then filter ignore it.

    arr.filter((item) => {
       return item.parent;
    }),
    
    let arr = [
        {
          id:1,
          parent:null,
          value1:22,
          value2:33
        },
        {
          id:2,
          parent:1,
          value1:0,
          value2:0
        },
        {
          id:3,
          parent:1,
          value1:40,
          value2:70
        },
        {
          id:4,
          parent:2,
          value1:40,
          value2:70
        },
        {
          id:5,
          parent:null,
          value1:40,
          value2:70
        }
    ]
    
    console.log(arr.filter((item) => {
            return item.parent;
    }))
    Login or Signup to reply.
  3. Just to an Array.filter

    const data = [
      { id: 1, parent: null, value1: 22, value2: 33 },
      { id: 2, parent: 1, value1: 0, value2: 0 },
      { id: 3, parent: 1, value1: 40, value2: 70 },
      { id: 4, parent: 2, value1: 40, value2: 70 },
      { id: 5, parent: null, value1: 40, value2: 70 },
    ];
    const output = data.filter((node) =>
      data.find((item) => node.parent && item.id === node.parent)
    );
    console.log(output);
    Login or Signup to reply.
  4. 
        const data =[
     {id:1, parent:null, value1:22, value2:33 }, 
     {id:2, parent:1, value1:0, value2:0 },
     {id:3, parent:1, value1:40, value2:70 },
     {id:4, parent:2, value1:40, value2:70 },
     {id:5, parent:null, value1:40, value2:70 }];
    
        const arr = data.filter(x => x.parent !== null && data.find(y => y.id === x.id));
        console.log(arr);
    
    
    Login or Signup to reply.
  5. This should work by creating an array of ids and filtering by it

    const array =[
    {
      id:1,
      parent:null,
      value1:22,
      value2:33
    },
    {
      id:2,
      parent:1,
      value1:0,
      value2:0
    },
    {
      id:3,
      parent:1,
      value1:40,
      value2:70
    },
    {
      id:4,
      parent:2,
      value1:40,
      value2:70
    },
    {
      id:5,
      parent:null,
      value1:40,
      value2:70
    }]
    
    const ids = []
    array.forEach(item => 
    {
    ids.push(item.id);
    }
    );
    const result = array.filter(el => ids.includes(el.parent));
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search