Home > other >  loop through an array and check if array inside it has a certain value
loop through an array and check if array inside it has a certain value

Time:08-26

I have an array of objects like below

const arr = 
  [ { a: '', b: '', arr: [ { label: '2' }, { label: '3' }                 ] } 
  , { a: '', b: '', arr: [ { label: '1' }, { label: '2' }, { label: '3' } ] } 
  , { a: '', b: '', arr: [ { label: '1' }, { label: '3' }                 ] } 
  ] 

I need to filter this array and get the objects in which the arr array has the label value of 2.

ie., expected result :

const arr = 
  [ { a: '', b: '', arr: [ { label: '2' }, { label: '3' }                 ] } 
  , { a: '', b: '', arr: [ { label: '1' }, { label: '2' }, { label: '3' } ] } 
  ] 

I have tried something like this:

array.forEach((item) => item.arr.filter(i) => i.label === '2')

how would we get back a filtered array looping through this array which has label values as "2" inside the arr array?

How can I achieve the expected result?

CodePudding user response:

With filter, some and destructuring

const arr = 
  [ { a: '', b: '', arr: [ { label: '2' }, { label: '3' }                 ] } 
  , { a: '', b: '', arr: [ { label: '1' }, { label: '2' }, { label: '3' } ] } 
  , { a: '', b: '', arr: [ { label: '1' }, { label: '3' }                 ] } 
  ]
  
const output = arr.filter(({ arr }) => arr.some(({ label }) => label === '2'));
  
console.log(output)

CodePudding user response:

Like this:

let oldArray = array = [
   {
      "a":"",
      "b":"",
      "arr":[
         {
            "label":"2"
         },
         {
            "label":"3"
         }
      ]
   },
{
      "a":"",
      "b":"",
      "arr":[
         {
            "label":"1"
         },
         {
            "label":"2"
         },
         {
            "label":"3"
         }
      ]
   },
{
      "a":"",
      "b":"",
      "arr":[
         {
            "label":"1"
         },
         {
            "label":"3"
         }
      ]
   }
]

let newArray = oldArray.filter(item=>
  item.arr && item.arr.filter(inner=> inner.label == "2").length >= 1
)

console.log(newArray)

CodePudding user response:

just use Array.some() and Array.splice() methods to remove undesired elements

const arr = 
  [ { a: '', b: '', arr: [ { label: '2' }, { label: '3' }                 ] } 
  , { a: '', b: '', arr: [ { label: '1' }, { label: '2' }, { label: '3' } ] } 
  , { a: '', b: '', arr: [ { label: '1' }, { label: '3' }                 ] } 
  ] 
  
for (let i=arr.length; i--;) 
  {
  if (!arr[i].arr.some(x=>x.label==='2'))
    arr.splice(i,1)
  }
  
console.log( arr ) 
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}


Or Array.filter() and Array.some() methods to create a new array with only desired elements

const arr = 
  [ { a: '', b: '', arr: [ { label: '2' }, { label: '3' }                 ] } 
  , { a: '', b: '', arr: [ { label: '1' }, { label: '2' }, { label: '3' } ] } 
  , { a: '', b: '', arr: [ { label: '1' }, { label: '3' }                 ] } 
  ] 
  
const newArr = arr.filter(el => el.arr.some(x=>x.label==='2'))
 
console.log( newArr )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

  • Related