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

101
Visualizações
Combine unknown sub-arrays of objects from an array of objects and filter it

a simpler way to re-create the output of this code, where if sub arr0 is not null, it then combines it and filter desired output by the key code.

Thank you in advance!

let array = [
  {
    id: 1,
    text: 'stuff1',
    arr0: [
      {id:1, code: 'imacode1'},
      {id:2, code: 'imacode2'},
    ]
  },
  {
    id: 2,
    text: 'stuff2',
    arr0: [
      {id:3, code: 'imacode3'},
      {id:4, code: 'imacode4'},
    ]
  },
  {
    id: 3,
    text: 'stuff3',
    arr0: []
  }
]
let arr = [];

for(let i of array){
  if(i.arr0.length !== 0){
    arr.push(i.arr0)   
  }
}
arr = arr.flat()
for(let j of arr){
  if(j.code === 'imacode2'){
    let code = arr.filter(j=>j.code!=='imacode2')
    code = code.map(({code}) => code)
    console.log(code)
  }
}
    

edit: added snippet

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You oan use Array.flatMap() along with with Array.filter() to get the result you wish, first we use .flatMap() to create an array including all items in each arr0.

We then use .filter() to only include the desired items, using a custom (modifiable) filter function, in this case removing any item with a code of 'imacode2'.

let array = [ { id: 1, text: 'stuff1', arr0: [ {id:1, code: 'imacode1'}, {id:2, code: 'imacode2'}, ] }, { id: 2, text: 'stuff2', arr0: [ {id:3, code: 'imacode3'}, {id:4, code: 'imacode4'}, ] }, { id: 3, text: 'stuff3', arr0: [] } ]

// Change filter as desired..
const filterProc = ({id, code}) => code !== 'imacode2';
const result = array.flatMap(el => el.arr0 || []).filter(filterProc).map(({code}) => code);
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza 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