forked from seeditsolution/javaprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch function
36 lines (28 loc) · 846 Bytes
/
search function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function search(searchString) {
//we test if searchString is empty in that case we just return the original data
if (typeof searchString !== 'string' || searchString.length === 0) {
return bands;
}
//we make search string lower case
let searchLower = searchString.toLowerCase();
let filtered = bands.filter(band => {
if (band.name.toLowerCase().includes(searchLower)) {
return true;
}
if (band.description.toLowerCase().includes(searchLower)) {
return true;
}
//now we search in albums as well; we store values in an array
let filteredAlbums = band.albums.filter(album => {
if (album.name.toLowerCase().includes(searchLower)) {
return true; //this is a return for albums
}
return false; //this is a return for albums
});
if (filteredAlbums.length > 0) {
return true;
}
return false;
})
return filtered;
}