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

176
Views
How can I filter an array by its properties?

I have a headless CMC WP that's fetched into my Javascript and stored. I want to filter it by a property(if the property value = to the value I input in html then it should append to the innerHTML) and therefore I tried to loop through it but it does not work. This is my function:

for (let i in this.filtered) {
  for (let y in this.filtered[i].categories) {
    //console.log(this.filtered[i].categories[y] + " ");
    const results = this.filtered[i].categories[y].filter(post => post == value)
    console.log(results);
  }
}

It says in the console that this.filtered[i].categories[y].filter is not a function.

this is a part of the array from this.filtered, I want to filter by categories. enter image description here

This is what it shows when I console.log(this.filtered[i].categories[y]); enter image description here

Thank you in advance!

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

0

It says in the console that this.filtered[i].categories[y].filter is not a function.

The items in the categories array, according to your screenshot, are simple integers. Integers don’t understand/implement any method called filter(). (Put another way: what would you do if I told you to filter the number 7?) Logically, it’s clear that this is not what you intended.

While you’re headed down the right path, I’d recommend you keep it simple: run Array.filter on your outermost array and use the logic in the callback to test whether the categories sub-array contains the value you’re looking for:

var _this = {};
_this.filtered = [{
    acf: {},
    author: 1,
    categories: [18, 2, 3]
  },
  {
    acf: {},
    author: 2,
    categories: [4, 3, 7, 18]
  }
];

const value = 2;

var filteredResults = _this.filtered.filter(cv => cv.categories.includes(value));

console.log(filteredResults);

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!