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

111
Visualizações
How do I filter an array while excluding specific conditions?

I have button that I’m disabling by reading an array and checking its properties.

My array looks like this:

const arr = [
  {id: ‘1’, name: ‘SOME_THING’, status: ‘AGREED’},
  {id: ‘2’, name: ‘THIS_THING’, status: ‘AGREED’},
  {id: ‘3’, name: ‘OTHER_THING’, status: ‘AGREED’},
  {id: ‘4’, name: ‘EVERY_THING’, status: ‘AGREED’},
]; 

My current logic to disable/enable this button looks like this:

arr.filter((item) => item.indexOf(‘THING’) > -1)
   .every((item) => item.status === ‘AGREED’)

Now my requirements have changed. I want to enable/disable the button in the same way, but I want to EXCLUDE specific elements from this array. For example, I want to exclude these two items:

{id: ‘2’, name: ‘THIS_THING’, status: ‘AGREED’},
{id: ‘3’, name: ‘OTHER_THING’, status: ‘AGREED’},

Using my logic, how do I filter my array while EXCLUDING these two?

I realize I can just try to delete these specific items but I was wondering if there was potentially a cleaner way to do that without explicit deletion.

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

0

You can exclude a list of items by always assuming the given key/value pair is true regardless of the value

const exclusionList = ['THIS_THING', 'OTHER_THING'];

arr.filter((item) => item.indexOf(‘THING’) > -1)
   .every((item) => item.status === ‘AGREED’ || exclusionList.includes(item.name))
about 4 years ago · Juan Pablo Isaza Relatório

0

Also please add your keyName(name) Where you using indexOf

let arr = [
      {id: '1', name: 'SOME_THING', status: 'AGREED'},
      {id: '2', name: 'THIS_THING', status: 'AGREED'},
      {id: '3', name: 'OTHER_THING', status: 'AGREED'},
      {id: '4', name: 'EVERY_THING', status: 'AGREED'},
      
    ]; 
    const list = ['THIS_THING', 'OTHER_THING'];
    console.log(arr.filter((item) => item.name.indexOf(‘THING’) > -1).every((user) => user.status === 'AGREED' || list.some(user.name)))
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