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

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

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