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

107
Views
JavaScript: filter array of objects by another

I'm trying to filter some objects based on another array of objects. So I'm getting data from an API. These are for example receipts:

[
  {
    "id": 1,
    "name": "test",
    "category": {
        "id": 1,
        "name": "Cookies",
    },
  },
  {
    "id": 2,
    "name": "test2",
    "category": {
        "id": 2,
        "name": "Candy",
    },
  }
]

Then I'm trying to filter the objects on the category name based on another array of categories. I've created a function for this:

function onSelectCategory(category) {
  let receiptsList = receipts.filter((a) =>
    a.category.includes(category.name)
  );
  setReceiptsView(receiptsList);
  setSelectedCategory(category);
}

const category = [ { "id": 2, "name": "Candy" } ];
onSelectCategory(category);

When I run this function, I get an empty Array []. I can't really figure out what I'm doing wrong.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Since the param seems to be an array of objects, you need to use Array#some for comparison instead:

const receipts = [
  { "id": 1, "name": "test", "category": { "id": 1,  "name": "Cookies" } },
  { "id": 2, "name": "test2", "category": { "id": 2, "name": "Candy" } }
];
const categories = [ { "id": 2, "name": "Candy" } ];

const receiptsList = receipts.filter(({ category }) => 
  categories.some(({ name }) => name === category.name)
);

console.log(receiptsList);

Another solution using Set:

const receipts = [
  { "id": 1, "name": "test", "category": { "id": 1,  "name": "Cookies" } },
  { "id": 2, "name": "test2", "category": { "id": 2, "name": "Candy" } }
];
const categories = [ { "id": 2, "name": "Candy" } ];

const categorySet = new Set(categories.map(({ name }) => name));

const receiptsList = receipts.filter(({ category }) => 
  categorySet.has(category.name)
);

console.log(receiptsList);

about 4 years ago · Juan Pablo Isaza Report

0

Assuming that category (the parameter) is a string, the issue is that you are attempting to get the attribute name from the string, when you should be comparing the string to the object.

Try this:

a.category.name == category;

instead of

a.category.includes(category.name)

I may be wrong aboout assuming that category is a string, please clarify by telling us what the parameter category is equal to.

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!