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

171
Visualizações
Delete multiple elements by properties from array by list of properties

I have a problem that I cannot solve and it seems a little strange to me. Having a list of objects foo, each object having a userId property, I want to filter the list to remove all items containing such userId that are in the bar array. I used the filter() and some() methods, however, they only work when the bar array has only one element. Does some() somehow stop the entire filter from running when it finds the first matching element? I would be thankful if you could explain to me why this does not work.

Example code:


let foo = [
  {
    "userId": 1,
  },
  {
    "userId": 2
  },
  {
    "userId": 3
  },
  {
    "userId": 4
  }
];

let bar = [1, 2];

console.log(foo.filter(user => bar.some(id => id !== user.userId)));

Expected value:

[
  {
    "userId": 3
  },
  {
    "userId": 4
  }
]

But I got:

[
  {
    "userId": 1,
  },
  {
    "userId": 2
  },
  {
    "userId": 3
  },
  {
    "userId": 4
  }
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your condition currently checks if there is at least one item in the bar array which is not the same as the items array you are iterating over. That will be true for all items.

You need to check for the items that exist using some() and based on that return the inverse value .

let foo = [
  {
    "userId": 1,
  },
  {
    "userId": 2
  },
  {
    "userId": 3
  },
  {
    "userId": 4
  }
];

let bar = [1, 2];

console.log(foo.filter(user => !bar.some(id => id === user.userId)));

about 4 years ago · Juan Pablo Isaza Relatório

0

you can simply use includes methods to check this situation.

let foo = [
  {
    "userId": 1,
  },
  {
    "userId": 2
  },
  {
    "userId": 3
  },
  {
    "userId": 4
  }
];

let bar = [1, 2];
// filter out for user IDs not in bar
console.log(foo.filter(user => !bar.includes(user.userId)));

for more information about includes method: MDN

about 4 years ago · Juan Pablo Isaza Relatório

0

The some() method tests whether at least one element in the array passes the test.

Explanation of Why some() is not working here:

[1,2].some(id !== 1) will return true because id 2 !== 1

[1,2].some(id !== 2) will return true because id 1 !== 2

[1,2].some(id !== 3) will return true because id 1 !== 3

[1,2].some(id !== 4) will return true because id 1 !== 4

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