简体   繁体   中英

How to implement Continue Statement with Filter Function in JavaScript

As I'm new to JS i implemented filter function on an array. I want to skip some iterations . How can I implement continue statement etc in filter function

Filter takes a callback function that returns a boolean, it doesn't skip iterations. If you would like to exclude an element from your result, your callback function should return false.

Ie if I wanted to filter all odd numbers from an array:

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const evenNumbers = numbers.filter(number => number % 2 === 0); // [2, 4, 6, 8]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM