简体   繁体   English

如何在JavaScript中使用过滤器函数实现Continue语句

[英]How to implement Continue Statement with Filter Function in JavaScript

As I'm new to JS i implemented filter function on an array. 因为我是JS新手,所以我在数组上实现了过滤器功能 I want to skip some iterations . 我想skip some iterations How can I implement continue statement etc in filter function 如何在过滤器函数中实现continue statement etc

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. 如果要从结果中排除某个元素,则回调函数应返回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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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