xxxxxxxxxx
function sum(numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
xxxxxxxxxx
// an array
const array = [ 1, 2, 3, 4, 5 ];
// check if any of the elements are less than three (the first two are)
array.some(function(element) {
return element < 3;
}); // -> true
// ES6 equivalents
array.some((element) => {
return element < 3
}); // -> true
array.some((element) => element < 3); // -> true
xxxxxxxxxx
let array = [1, 2, 3, 4, 5];
//Is any element even?
array.some(function(x) {
return x % 2 == 0;
}); // true
xxxxxxxxxx
var nums = [1, 2, 3, 4, 5, 6, 7];
function even(ele)
{
return ele%2 == 0;
}
console.log(nums.some(even))
/*consol.log will show true because at least one of the elements is even*/
xxxxxxxxxx
function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true