Answers for "how to apply limit in filter javascript"

0

how to apply limit in filter javascript

const arr = [1,0,2,0,3,0,4,5,6,7,8,9,10,11,12,13,14];
const filtered = arr.filter(function(item) {
  if (this.count < 1 && item > 0) {
    this.count++;
    return true;
  }
  return false;
}, {count: 0});

console.log(filtered);
 Run code snippetHide results
Posted by: Guest on March-23-2022

Code answers related to "how to apply limit in filter javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language