标签:
function square(num) { alert(num + " : " + num * num); } var nums = [ 1, 2, 3, 4 ]; nums.forEach(square);
function isEven(num) { return num % 2 == 0; } var nums = [ 2, 4, 6, 8, 10 ]; var even = nums.every(isEven); if (even) { alert("all numbers are even"); } else { alert("not all numbers are even"); }
function isEven(num) { return num % 2 == 0; } var nums = [ 1, 2, 3, 6, 7 ]; var someEven = nums.some(isEven); if (someEven) { alert("some numbers are even"); } else { alert("no numbers are even"); }
function add(runningTotal, currentValue) { return runningTotal + currentValue; } var nums = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; var sum = nums.reduce(add); alert(sum); // 显示 55
function concat(accumulatedString, item) { return accumulatedString + item; } var words = [ "the ", "quick ", "brown ", "fox " ]; var sentence = words.reduce(concat); alert(sentence); // 显示 "the quick brown fox"
function add(runningTotal, currentValue) { return runningTotal + currentValue; } var nums = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; var sum = nums.reduceRight(add); alert(sum); // 显示 55
function concat(accumulatedString, item) { return accumulatedString + item; } var words = [ "the ", "quick ", "brown ", "fox " ]; var sentence = words.reduceRight(concat); alert(sentence); // 显示 "fox brown quick the"
不生成新数组的迭代器方法:forEach()&every()&some()&reduce()&reduceRight()
标签:
原文地址:http://www.cnblogs.com/feile/p/5369987.html