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

111
Views
Filtering an array of string within an array of objects with an array of string

Sorry if it's been asked before but i couldn't find a solution despite searching in the website. I have an array of objects (restaurants) with many properties. One of them (tags) is an array of string. Then i have another array of strings (filters). The result i need after filtering is an array of restaurants whose tags includes all the filters (this is made reactive with VueJS, there can be any amount of filters including zero).

I'm pretty sure there is a simple solution that i'm currently missing, combining some loops, filter() and some()/includes().

Here is the closest I've been to the result i want (in what probably is a very inefficient way), but it's buggy if you choose some pattern of filter in a certain combination and order(it splice a restaurant that shouldn't be spliced, I have no clue why).

  if (restaurants) {
    filters.forEach((filter) => {
      for (let i = 0; i < restaurants.length; i++) {
        const restaurant = restaurants[i]

        if (!checkedFilters.includes(filter)) {
          checkedFilters.push(filter);
        }

        if (restaurant.tags.some((tag) => tag.name === filter)) {
          if (!filteredRestaurants.includes(restaurant)) {
            filteredRestaurants.push(restaurant)
          }
        }
      }
    })

    for (let i = 0; i < filteredRestaurants.length; i++) {
      const filteredRestaurant = filteredRestaurants[i];

      checkedFilters.forEach((checkedFilter) => {
        if (!filteredRestaurant.tags.some((tag) => tag.name === checkedFilter)) {
          filteredRestaurants.splice(i, 1)
        }
      })
    }

    return filteredRestaurants
  }
},

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

0

I'm not sure why you have a loop to remove things. Does filteredRestaurants already contain restaurants before running this code? If not, try:

Edit: modified to only match restaurants that pass every filter condition.

const restaurants = [
  {name: "r1", tags: ["fastfood", "chicken", "fish"]},
  {name: "r2", tags: ["steak", "chicken", "seafood"]},
  {name: "r3", tags: ["fastfood", "burger", "sandwich"]},
  {name: "r4", tags: ["steak", "burger", "sandwich"]}
];

const filters = ["steak", "seafood"];

let filteredRestaurants = restaurants.filter(r => filters.every(f => r.tags.includes(f)));

console.log(filteredRestaurants);

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!