简体   繁体   中英

Search in array of objects in JavaScript in all keys values

Can somebody give an example of good performance optimized search function in array of objects in javascript. I am trying to make a serachbar which returns a matched value in array of objects but i can only make it to return value if it match a certain key in my example is 'name'. Want to make a search which returns values in all the keys. This is my example object, and this is my search function. Thanks in advance

{{name: 'John Smith', email: 'johnsmith@smth.com', organization: 'Nike'},{name: 'John Doe', email: 'johndoe@smth.com', organization: 'Sony'}}

filteredItems() {
  return this.items.filter(item => {
   return (item.name.toLowerCase().indexOf(this.filter.toLowerCase()) > -1)
})}
const filteredItems = () => {
  const regex = new RegExp(this.filter, 'i');
  return this.items.filter((item) => Object.values(item).find((value) => regex.test(value)));
};

I believe this should be somewhat optimised for your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM