Javascript search item from an array of objects

Using jquery grep to search an object from an array with a value of a specific key.

For example searching like mysql 'like'
var restaurants [{id:1, restname: 'Cafe Namo'},{id:2,restname:'TefeCool'},{id:3,restname:'Ivans Resto'}];

var query_string = 'fe';


$.grep(restaurants, function (e) {
 var search = new RegExp(query_string, "i");
 return search.test(e.restname);
});

Return value is
[{id:1, restname: 'Cafe Namo'},{id:2,restname:'TefeCool'}]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s