遍历

arr.forEach

在ES5中遍历数组,通常都是使用for循环

  1. var arr = [1,2,3];
  2. for(var i=0; i<arr.length; i++) {
  3. console.log(i);
  4. }

数组提供一个方法:forEach,来代替for循环
forEach方法没有返回值,它传入一个回调函数
这个回调函数接受两个参数,item(数组中的每个元素)和index

  1. var arr = [1,2,3];
  2. var arr = [1,2,3];
  3. arr.forEach(function(item , index) {
  4. console.log(item + '-' + index);
  5. });

for…of

  1. var arr = [1,2,3,4,5,6,7,9,10,11,'a'];
  2. for(let val of arr) {
  3. console.log(val); //每一项元素值
  4. }
  5. for(let index of arr.keys()) {
  6. console.log(index); //每一项元素的下标
  7. }
  8. for(let index of arr.entries()) {
  9. console.log(index); //每一项元素的下标和值,以数组的形式呈现
  10. }
  11. //解构的方式
  12. for(let index of arr.entries()) {
  13. console.log(index); //每一项元素的下标和值,以数组的形式呈现
  14. }

数组方法

arr.map

arr.map的作用:遍历数组的每一项,通过回调函数处理返回一个新的数组
arr.map方法返回一个新的数组,它接受一个回调函数
这个回调函数接受两个参数,item(数组中的每个元素)和index,并且函数的结尾必须return一个值或者对象

例如:每个数组元素都+1

  1. var arr = [1,2,3];
  2. var arr1 = arr.map(function(item , index) {
  3. return item+1;
  4. });
  5. console.log(arr1); //[2, 3, 4]

arr.filter

arr.filter的作用:遍历数组的每一项,只返回符合条件的元素
arr.filter方法返回一个新的数组,它接受一个回调函数
这个回调函数接受两个参数,item(数组中的每个元素)和index,处理结果为true的返回到新的数组,为false则不返回

例如:提取数组中为3的倍数的数字

  1. var arr = [1,2,3,4,5,6,7,9,10,11,12];
  2. var arr1 = arr.filter(function(item , index) {
  3. return item%3==0;
  4. });
  5. console.log(arr1); //[3, 6, 9, 12]

arr.some

arr.some的作用:判断数组中的某个元素是否符合条件,只要有一个符合则返回true
实例:检查数组中是否存在字符串类型的元素

  1. var arr = [1,2,3,4,5,6,7,9,10,11,'a'];
  2. var arr1 = arr.some(function(item , index) {
  3. return typeof item=='string';
  4. });
  5. console.log(arr1); //true

arr.every

arr.every的作用:判断数组中的所有元素是否符合条件,所有元素都符合都返回true
实例:检查数组中所有元素都是字符串类型的元素

  1. var arr = [1,2,3,4,5,6,7,9,10,11,'a'];
  2. var arr1 = arr.every(function(item , index) {
  3. return typeof item=='string';
  4. });
  5. console.log(arr1); //false


arr.reduce

arr.reduce的作用,作为累加器,合并数组中的每个值,最终返回一个值
它接受一个回调函数,这个回调函数接收三个值:

  • pre :上一次调用回调返回的值 或者是提供的初始值。
  • cur : 数组中当前被处理的数组项
  • index :当前数组项在数组中的索引值

一个比较简单的例子,数组求和

  1. let arr = [1,2,3,4,5,6,7,8,9,10];
  2. let sum = arr.reduce(function(pre , cur , index) {
  3. return pre+cur;
  4. });

arr.from

arr.from把类数组的对象转换成数组类型
简单实例:

  1. function show() {
  2. console.log(arguments); //Arguments(5) [1, 2, 3, 4, 5]
  3. let arr = Array.from(arguments);
  4. console.log(arr); //(5) [1, 2, 3, 4, 5]
  5. }
  6. show(1,2,3,4,5);

复制数组:

  1. let arr = [1,2,3,4];
  2. let arr1 = Array.from(arr);

打散字符串:

  1. let str = 'Hello';
  2. let arr1 = Array.from(str); //(5) ["H", "e", "l", "l", "o"]

arr.of

arr.of 把一组值转换成功数组

  1. let arr1 = Array.of(1, 'Hell', true);
  2. console.log(arr1); //(3) [1, "Hell", true]

arr.find 和 arr.findIndex

arr.find 找到第一个符合条件的元素
它接受一个回调函数,回调函数接受两个参数:val , index
找到符合条件的元素则返回,找不到则返回undefined

  1. let arr1 = Array.of(1, 'Hell', true);
  2. console.log(arr1); //(3) [1, "Hell", true]
  3. let result = arr1.find(function(item , index) {
  4. return typeof item == 'boolean';
  5. });
  6. console.log(result); //true
  7. let resultIndex = arr1.findIndex(function(item , index) {
  8. return typeof item == 'boolean';
  9. });
  10. console.log(resultIndex); //2

arr.includes

arr.includes 判断数组中是否存在某个元素

  1. let arr = ['a' , 'b' , 'c'];
  2. console.log(arr.includes('a')); //true
  3. console.log(arr.includes('aa')); //false