1.while
let index = 0;
const array = [1,2,3,4,5,6];
while (index < array.length) {
console.log(array[index]);
index++;
}
2.for ()
const array = [1,2,3,4,5,6];
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
}
3.forEach
const array = [1,2,3,4,5,6];
array.forEach(function(current_value, index, array) {
console.log(`在数组下标 ${index} 处数组 ${array} 的值是 ${current_value}`);
});
// => undefined
4.map
const array = [1,2,3,4,5,6];
const square = x => Math.pow(x, 2);
const squares = array.map(square);
console.log(`原始数组: ${array}`);
console.log(`平方后的数组: ${squares}`);
5.reduce
const array = [1,2,3,4,5,6];
const sum = (x, y) => x + y;
const array_sum = array.reduce(sum, 0);
console.log(`数组 ${array} 加起来的和是: ${array_sum}`);
6.filter
const array = [1,2,3,4,5,6];
const even = x => x % 2 === 0;
const even_array = array.filter(even);
console.log(`数组 ${array} 中的偶数是: ${even_array}`);
7.every
const array = [1,2,3,9,5,6];
const under_seven = x => x < 7;
if (array.every(under_seven)) {
console.log('数组中每个元素都小于7');
} else {
console.log('数组中至少有一个元素大于7');
}
8.some
const array = [1,2,3,7,5,6,4];
const over_seven = x => x > 7;
if (array.some(over_seven)) {
console.log('至少有一个元素大于7');
} else {
console.log('没有元素大于7');
}