I am trying to implement a search functionality that filters an array and displays only the matching results.
let songs = [
{name: 'Let It Be', artist: 'The Beatles},
{name: 'Lady Madonna', artist: 'The Beatles },
{name: 'Mama Mia', artist: 'The Beatles}
];
In this example, the filtering is done based on the song name.
let value = 'name';
let q = 'Let it be' // user input entered in the search box;
let songFilter = songs.filter(function(song) {
return song[value].toString().toLowerCase().indexOf(q) != -1; // returns true or false
});
Currently, entering Let It Be
in the search box will only display 'Let It Be'
. But I want to be able to filter by multiple songs. For instance, entering Let It Be, Lady Madonna
should return both matching songs.
I have tried various methods but haven't been successful in achieving this. I also have access to lodash
which might help simplify the solution.