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

221
Visualizações
How can I search only in specifics object.keys in a array in Javascript?

I have the following array:

arr = [
  { id: '1', name: 'Something', code: 'A33', number: '67', house: 'St55' },
  { id: '2', name: 'Another', code: '55', number: '004', house: 'ES' },
  { id: '3', name: 'One more', code: 'DAE', number: '003', house: 'R5' },
];

I'm using the function bellow (source) for a search bar to search all fields of array of objects (arr) depends on the user inputs (value):

function findInValues(arr, value) {
  value = String(value).toLowerCase();
  return arr.filter(o =>
    Object.entries(o).some(entry =>
      String(entry[1]).toLowerCase().includes(value)
    )
  );
}

But to resolve my case, the function would can search only on the keys name and house, the search must ignore id,code and number.

For example:

value = 55

Result = [
      { id: '1', name: 'Something', code: 'A33', number: '67', house: 'St55' }
     ];

What should I do in my case?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can destructure the keys id, code and number that you want to ignore from the object you're filtering and then grab the remaining object without these properties using the rest syntax ...o. You can then use the same code that you're currently using to search the o object. Note that below, I have also changed Object.entries() to be just Object.values() as your code isn't using the key from the entry array:

const arr = [
  { id: '1', name: 'Something', code: 'A33', number: '67', house: 'St55' },
  { id: '2', name: 'Another', code: '55', number: '004', house: 'ES' },
  { id: '3', name: 'One more', code: 'DAE', number: '003', house: 'R5' },
];

function findInValues(arr, value) {
  value = String(value).toLowerCase();
  return arr.filter(({id, code, number, ...o}) =>
    Object.values(o).some(val =>
      String(val).toLowerCase().includes(value)
    )
  );
}

console.log(findInValues(arr, 55));

Otherwise, if you know the keys you want to search (ie: name and house), then you can check those explicitly:

const arr = [
  { id: '1', name: 'Something', code: 'A33', number: '67', house: 'St55' },
  { id: '2', name: 'Another', code: '55', number: '004', house: 'ES' },
  { id: '3', name: 'One more', code: 'DAE', number: '003', house: 'R5' },
];

function findInValues(arr, value) {
  value = String(value).toLowerCase();
  const search = ['name', 'house'];
  return arr.filter(o => search.some(key => o[key].toLowerCase().includes(value)));
}

console.log(findInValues(arr, 55));

about 4 years ago · Juan Pablo Isaza Relatório

0

Shorter version! :)

function findInValues(arr, value) {
    value = String(value).toLowerCase();
    return arr.filter(({
            name,
            house
        }) =>
        (name.toLowerCase() == value || house.toLowerCase() == value)
    );
}
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