Skip to content

Filter on all fields in an array of objects

We know how to filter on an array of objects on specific fields. In some cases we might want to filter on all fields. This code snippet would help.

var records = [
  { id : 1 ,  name : 'abc' , phone : 1234}, 
  { id : 2 ,  name : 'abd' , phone : 3456}, 
  { id : 3 ,  name : 'xyz' , phone : 5678}
];

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

console.log(findInValues(records,  3));
console.log(findInValues(records, 'a'));
console.log(findInValues(records, 'z'));
console.log(findInValues(records, 567));
See also  Remove falsy values from an array in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.