Can someone help me with filtering an array of objects in javascript? I have an array of users
such as:
var users = [
{
first: 'Jon',
last: 'Snow',
email: 'jon@got.com'
},
{
first: 'Ned',
last: 'Stark',
email: 'ned@got.com'
},
{
first: 'tywin',
last: 'Lannister',
email: 'tywin@got.com'
},
]
And I'm trying to write a function to search through the array of objects:
function search(str, users) {
var results = users.filter(function(el) {
return (el.first === str || el.last === str || el.email === str);
});
return results;
}
However, doing it this way, my search parameter str
would have to exactly match the first/last/email value of the user array.. I need the function be able to search based on a substring... Can someone help?
Thanks in advance!