我是表哥Harker,表妹我来咯~
这篇介绍 ES5 新增方法(数组方法 、字符串方法、对象方法),能更好的去控制这些类型。
1、数组方法
- 迭代(遍历)方法:forEach()、map()、filter()、some()、every()
1)、forEach() 遍历数组中每个元素,可以添加自己的一些逻辑
- forEach() 对于空数组是不会执行回调函数的
- 不需要return,即使有return也不起作用
array.forEach(function(currentValue, index, arr))
- currentValue:数组当前项的值
- index:数组当前项的索引
- arr:数组对象本身
<script>
// forEach 迭代(遍历) 数组
var arr = [1, 2, 3];
var sum = 0;
arr.forEach(function(value, index, array) {
console.log('数组元素' + value);
console.log('数组元素的索引号' + index);
console.log('数组本身' + array);
sum += value;
})
console.log(sum); // 6
</script>