searchArrayList = searchArray.split(' ');
filteredArray = person.filter(function (item) {
let row = [item.fname != null ? item.fname.toLowerCase() : '',
item.lName != null ? item.lName.toLowerCase() : '',
item.age != null ? item.age.toLowerCase() : ''].join();
let result = searchArrayList.map(function (part) {
if (part === '') return true
return row.indexOf(part.toLowerCase()) !== -1
})
return result.reduce(function (x, y) {
return x&y
}, true)
})
EXPLAINATION:
-
Split the searchArray string “John ABC 55” into Array
['John', 'ABC', '55']
searchArrayList = searchArray.split(' ');
-
JOIN()
method will cause the Person array item into single string :[{"John2", "ABC", 55}] => "John1 ABC 55"
-
Map the search result into boolean Array :
["John1 ABC 55"].indexOf("John") => 'true', ["John1 ABC 55"].indexOf("ABC") => 'true', ["John1 ABC 55"].indexOf("55") => 'true'
Map result => [true, true, true]
-
Reduce will result into single boolean array :
true