Reputation: 357
I am looking for an elegant way to generate booleans that will eventually be joined using && operator inside my callback function in filter method.
I tried to loop through the filter conditions but I cannot find a way to join each iteration result into the following format:
return Boolean && Boolean && Boolean && Boolean && Boolean
Becasue += && Boolean doesn't work.
Here is what I have and what is working:
//data I am filtering
this.preSearch = [
["The Lord of the Rings", "J. R. R. Tolkien", "English", "1955", "150 milionów"],
["Le Petit Prince (The Little Prince)", "Antoine de Saint-Exupéry", "French", "1943", "140 milionów"],
["Harry Potter and the Philosopher's Stone", "J. K. Rowling", "English", "1997", "120 milionów"],
["The Hobbit", "J. R. R. Tolkien", "English", "1937", "100 milionów"],
["And Then There Were None", "Agatha Christie", "English", "1939", "100 milionów"],
["Dream of the Red Chamber", "Cao Xueqin", "Chinese", "1791", "100 milionów"]
]
//filters, that are set dynamically but let's pretend they are equal to
var filters = ["", "", "english", "19", "1"]
var searchdata = this.preSearch.filter(row => {
return
row[0].toLowerCase().indexOf(filters[0].toLowerCase()) > -1
&& row[1].toLowerCase().indexOf(filters[1].toLowerCase()) > -1
&& row[2].toLowerCase().indexOf(filters[2].toLowerCase()) > -1
&& row[3].toLowerCase().indexOf(filters[3].toLowerCase()) > -1
&& row[4].toLowerCase().indexOf(filters[4].toLowerCase()) > -1
})
I need scalable and way more elegant solution so I will not have to add another line with && if I enhance my filtered array.
Upvotes: 1
Views: 97
Reputation: 386746
You could use Array#every
for the filters array.
For a faster check, you could convert the filter values to lowercase in advance.
var preSearch = [["The Lord of the Rings", "J. R. R. Tolkien", "English", "1955", "150 milionów"], ["Le Petit Prince (The Little Prince)", "Antoine de Saint-Exupéry", "French", "1943", "140 milionów"], ["Harry Potter and the Philosopher's Stone", "J. K. Rowling", "English", "1997", "120 milionów"], ["The Hobbit", "J. R. R. Tolkien", "English", "1937", "100 milionów"], ["And Then There Were None", "Agatha Christie", "English", "1939", "100 milionów"], ["Dream of the Red Chamber", "Cao Xueqin", "Chinese", "1791", "100 milionów"]],
filters = ["", "", "english", "19", "1"].map(s => s.toLowerCase()),
result = preSearch
.filter(row => filters.every((v, i) => row[i].toLowerCase().includes(v)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 3
Reputation: 5188
You can do this by applying Array.every()
and String.includes()
like this:
var searchdata = this.preSearch.filter(row => {
// this only returns true if our condition works for
// index = 0, 1, 2, 3, 4
return [0, 1, 2, 3, 4].every(index => {
const rowContent = row[index].toLowerCase();
const filterContent = filters[index].toLowerCase();
// String.includes() is nicer than String.indexOf() here because
// you don't need the ugly -1
return rowContent.includes(filterContent);
});
});
Upvotes: 1