Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda