var arr = [1,2,3,4,5];
arr.forEach(function(curval, index, array) {
var squ = curval * curval;
console.log('index is : ' + index + ', result is : ' + squ);
})
var mul2 = function(num) {
return num * 2;
}
var mapArr = arr.map(mul2);
console.log(mapArr); // [2,4,6,8,10]
var evenArr = function(num) {
return num % 2 === 0;
}
var filterArr = arr.filter(evenArr);
console.log(filterArr); // [2,4]
var everyArr = arr.every(evenArr);
console.log(everyArr); // false
var someArr = arr.some(evenArr);
console.log(someArr); // true
var findArr = arr.find(evenArr);
console.log(findArr); // 2
var findIndexArr = arr.findIndex(evenArr);
console.log(findIndexArr); // 1
var maxVal = function(lastVal, curVal) {
return lastVal > curVal ? lastVal : curVal;
}
var reduceArr = arr.reduce(maxVal);
console.log(reduceArr); // 5
网友评论