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

126
Visualizações
Exclude the list of objects listed on the variable from the filter

I am working with JSON data and I filter some of the data out using .filter, I want it to exclude some categories and some products, which I can easily do by individually specifying on my filter return, but I reckon the list goes long and it doesn't look feasible the way it is.

I think I don't have to add the example JSON here, just imagine it has "category" and "product" for each data. Have a look how I process the data:

var myfilter = myjson.filter(function(c) {
    return (c.category != 3 && c.category!= 5 && c.category!= 8 && c.category!= 11 && c.product != 191 && c.product != 139 && c.product != "string instead of int");
  });

^This above already does what I need. What I desire is to turn it into something like this:

var excludedcats = [3, 5, 8, 11]
var excludedproducts = [191, 139, "string instead of int"]

var myfilter = myjson.filter(function(c) {
    return (c.category != excludedcats && c.product != excludedproducts);
  });

I am not a javascript pro, but I feel like this works like 'if' conditions and it has to keep repeating c.category != and c.product != for each, so I thought about some sort of loop until it specifies all of them. But I might be totally lost, too.

I already did some research, but could be that since I don't even know all the technical terms either I failed to find what I'm looking for. Most relevant thing I could find was this, it didn't help me, just adding here to prove I researched 😅

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

0

Here's a little more generic version.

var myjson = [{
    category: 4,
    product: "string instead of int"
  },
  {
    category: 3,
    product: 0
  },
  {
    category: 2,
    product: "should keep"
  }
];

var exclusions = {
  category: [3, 5, 8, 11],
  product: [191, 139, "string instead of int"]
}


var myfilter = myjson.filter(function(c) {
  for (var key in exclusions) {
    for (var i = 0; i < exclusions[key].length; i++) {
      if (c[key] == exclusions[key][i]) {
        return false;
      }
    }
  }
  return true;

});

console.log(myfilter)

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you can leverage the includes method:

var myfilter = myjson.filter(function(c) {
  return !excludedcats.includes(c.category) && !excludedproducts.includes(c.product);
  //return (c.category != excludedcats && c.product != excludedproducts);
});

More on that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

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