发布于 4年前

js 判断数组是否至少有一项满足条件

// 如果提供的函数对数组任意一项返回 true 则返回 true
const any = (arr, fn = Boolean) => arr.some(fn);

// EXAMPLES
any([0, 1, 2, 0], x => x >= 2); // true
any([0, 0, 1, 0]); // true

// 例:是否有一位已成年的用户
let users = [{ age: 13 }, { age: 20 }, { age: 8 }]
all(users, it => it.age >= 18) // true

//来自  https://30secondsofcode.org/
©2020 edoou.com   京ICP备16001874号-3