I have created the following code to search and page through an array:
let data = ['banana1','banana2','apple1','apple2','apple3','apple4','apple5','apple6','apple7','apple8','apple9','apple10','apple11','apple12','strawberry1','strawberry2','strawberry3','strawberry4','strawberry5'];
let searchTerm = "app";
let skip = 5;
let take = 2;
let count = 0;
var filtered = data.filter( (item, index) => {
if(index < skip) return false;
if(count >= take) return false;
if(item.includes(searchTerm)){
count++;
return true;
};
return false;
});
console.log(filtered);
It works but once the take limit is reached if will keep looping for entire array.
Is there a way to exit filter when take limit is hit or a different more efficient way to write this code?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment