Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda