Collect.js every() 方法
every() 方法用于验证给定集合的所有元素并使用给定的真值测试。
句法:
collect(array).every()
参数: collect() 方法接受一个参数,该参数转换为集合,然后对其应用 every()函数,如果将其应用于对象集合,则可以获取元素。
下面的例子说明了 collect.js 中的 every() 方法:
示例 1:
Javascript
const collect = require('collect.js');
const arr = [2, 4, 6, 8, 10, 12, 14];
const collection = collect(arr);
if (collection.every(element => element % 2 == 0)) {
console.log('All elements are even');
} else {
console.log('All elements are not even');
}
Javascript
const collect = require('collect.js');
const arr = ['Geeks', 'GFG', 'GeeksforGeeks', 'Welcome'];
const collection = collect(arr);
if (collection.every(element => element.length > 4)) {
console.log('Every words contains more then 4 characters');
} else {
console.log('Every words not contains more then 4 characters');
}
输出:
All elements are even
示例 2:
Javascript
const collect = require('collect.js');
const arr = ['Geeks', 'GFG', 'GeeksforGeeks', 'Welcome'];
const collection = collect(arr);
if (collection.every(element => element.length > 4)) {
console.log('Every words contains more then 4 characters');
} else {
console.log('Every words not contains more then 4 characters');
}
输出:
Every words not contains more then 4 characters