Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!