Skip to content

Latest commit

 

History

History
33 lines (21 loc) · 1.05 KB

has-many.md

File metadata and controls

33 lines (21 loc) · 1.05 KB
标题 标签
hasMany(匹配数组是否含有满足给定函数条件的数组项) array(数组)

检查数组是否有多个与给定函数匹配的值。

  • 结合使用 Array.prototype.filter() 和 handler 来查找所有匹配的数组元素。
  • 使用 Array.prototype.length 检查是否有多个元素匹配 handler。
const hasMany = (arr, handler) => arr.filter(handler).length > 1;

ts 代码如下:

调用方式:

hasMany([1, 3], x => x % 2); // true
hasMany([1, 2], x => x % 2); // false

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/has-many.html"></iframe>