Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda