Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
loop through an array and check if array inside it has a certain value

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?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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)

about 4 years ago · Santiago Trujillo Report

0

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)

about 4 years ago · Santiago Trujillo Report

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!