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

97
Vistas
Filter array of objects whose specific properties contains a value

I'm trying to find the cleanest way to implement a filter to an array of objects depending on a string keyword. I need to return those objects whose only specific properties contains the string value.

Let's say I have the following array of objects:

 const products = [
      {
        name: 'car',
        price: 100,
        image: 'someurl'
        email: 'car@car.car'
        available: true,
      }, 
      {
        name: 'phone',
        price: 200,
        image: 'someurl'
        email: 'phone@phone.phone'
        available: false,
      }, 
      {
        name: 'bottle',
        price: 300,
        image: 'someurl'
        email: 'bottle@bottle.bottle'
        available: true,
      }, 
    ];

As mentioned here: Filter array of objects whose any properties contains a value

One of the cleanest way to match any value in an array of objects whose properties have different types would be:

function filterByValue(array, string) {
    return array.filter(o =>
        Object.keys(o).some(k => String(o[k]).toLowerCase().includes(string.toLowerCase())));
}

This filterByValue function will return those objects whose any properties match the string value.

However, I'd like to add some conditions, so it only iterates and look for some match at "name", "price" and "email" properties, not looking at "image" and "available" properties.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Make an array of the properties to look at, and look at them instead of using Object.keys.

const propsToCheck = ['name', 'price', 'email'];
function filterByValue(array, string) {
    return array.filter(o =>
        propsToCheck.some(k => String(o[k]).toLowerCase().includes(string.toLowerCase())
        )
    );
}
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