Home > database >  Javascript Self - Recursive loop | Find within Filter | Loop Within Same Array
Javascript Self - Recursive loop | Find within Filter | Loop Within Same Array

Time:10-06

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.

CodePudding user response:

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

CodePudding user response:

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
  }
]

CodePudding user response:

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

CodePudding user response:


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

CodePudding user response:

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

  • Related