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

121
Visualizações
a function for data filtration based on two arrays of strings in Javascript

I need some help with array filtration. I want to filter an array of objects based on:

Note: these arrays can be empty. When they are empty, the function should return the original array (without data filtration)

brands: ["brand 1", "brand 2", "brand 3", "brand 4"],
tags: ["tag1", "tag2", "tag 3", "tag 4"],

The array of objects which I want to do filter looks like this:

[
 {
    "tags": [
      "tag1"
    ],
    "price": 10.99,
    "name": "Sample name",
    "manufacturer": "brand 1",
  },
 {
    "tags": [
      "tag1", "tag2"
    ],
    "price": 10.99,
    "name": "Sample name",
    "manufacturer": "brand 1",
  },
 {
    "tags": [
      "tag1", "tag3", "tag4"
    ],
    "price": 10.99,
    "name": "Sample name",
    "manufacturer": "brand 4",
  },
 {
    "tags": [
      "tag1", "tag2"
    ],
    "price": 10.99,
    "name": "Sample name",
    "manufacturer": "brand1 ",
  },
]

the function I have looks like this doing filtration on the manufacturer only:

const obj = {
  brands: ["brand 1", "brand 2"],
  tags: ["tag1", "tag2", "tag 4"],
}

const filterArray = (obj, array) => {
  let newArray = []
  const byBrands = array.filter((item) =>
    obj.brands.includes(item["manufacturer"].toLowerCase())
  )

  if (byBrands.length > 0) {
    newArray = byBrands
  } else {
    newArray = array
  }

  return newArray;
}

I need a function to do filtration on tags and manufacturer at the same time.

Thanks, Stakoverflow :)

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You should use keys in your filter object that match properties in the objects to be filtered, otherwise there's no way to know which property compare. Other than that it's just a matter of checking if every() property in the filter object has some() matching entry in the object being filtered. The example ignores empty arrays in the filter object using an OR (||) short-circuit, and uses concat() to evaluate every property as an array.

(You can fine tune to make it case-insensitive, search for substrings, etc.)

const input = [{ "tags": ["tag1"], "price": 10.99, "name": "Sample name", "manufacturer": "brand 1", }, { "tags": ["tag1", "tag2"], "price": 10.99, "name": "Sample name", "manufacturer": "brand 1", }, { "tags": ["tag 4"], "price": 10.99, "name": "Sample name", "manufacturer": "brand 2", }, { "tags": ["tag 3"], "price": 10.99, "name": "Sample name", "manufacturer": "brand 1", },]

const obj = {
  manufacturer: ["brand 1", "brand 2"],
  tags: ["tag1", "tag2", "tag 4"],
  name: [], // ignores empty arrays
}

function filterProducts(array, filters) {
  return array.filter(p =>
    Object.entries(filters).every(([k, fs]) =>
      !fs.length || fs.some(f => [].concat(p[k]).some(t => t === f)))
  )
}

console.log(filterProducts(input, obj))

about 4 years ago · Santiago Trujillo 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