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

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

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 Denunciar

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