Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda