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

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

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 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