03.数组的遍历方法

141 阅读3分钟

一、数组的遍历方法

总共有八种方法: ** forEach;map;filter;find;findTndex;some;every;reduce**

1.forEach

  • 语法:数组.forEach(function(item , index , array ){});
  • 参数:
    • 参数1数组的每一项值
    • 参数2数组每一项对应的下标
    • 参数3原始数组
  • 作用:遍历数组;
  • 返回值:没有返回值 返回值是undefined 就算加了return 返回值也是undefined.
const arr = [ 1 , 2 , 3 , 4 , 1 , 2 , 37 , 9, 5 , 6 ];

arr.forEach( function( item , index , array ){
    console.log( item , index , array );
    
} )

运行结果:

pic01.png

2.map

  • 语法:数组.map(function(item , index , array ){});
  • 参数:
    • 参数1是数组的每一项的值
    • 参数2数组每一项对应的下标
    • 参数3是原始数组
  • 作用:映射数组;
  • 返回值:取决于返回一个和原数组数组长度一样的数组,返回值的数组的每一个值 也就是 return.

const arr = [ 1 , 2 , 3 , 4 , 1 , 2 , 37 , 9, 5 , 6 ];
const res1 = arr.map( function( value , index , array ){
// return value ;    
// return index ;
// return array ;
return value * 10 ;
} )
console.log(res1);

运行结果:

pic02.png

3.filter

  • 语法形式:数组.filter(function( value , index , array ){ return 操作的数据; })
  • 参数:
    • 参数1数组的每一项实际的值;
    • 参数2数组每一项实际的值对应的下标;
    • 参数3原始数组.
  • 作用:过滤数组;
  • 返回值:过滤数组后的新数组 过滤条件取决于参数的return怎么写.
const arr = [ 1 , 2 , 3 , 4 , 1 , 2 , 37 , 9, 5 , 6 ];
const res2 = arr.filter(function(value , index , array){
      return value > 3 ;
})
console.log(res2);

运行结果:

03.png

4.find

  • 语法形式:数组.find( function( value , index , array ){ return 满足条件的数值; } )
  • 参数:
    • 数组的每一项值
    • 数组的每一项实际的索引下标
    • 原始数组
  • 作用:查找数据;
  • 返回值:返回数组内查找满足条件的第一个数据,如果中没有这个数值的话,就返回undefined.
const arr = [ 1 , 2 , 3 , 4 , 1 , 2 , 37 , 9, 5 , 6 ];
const res3 = arr.find(function(value , index , array){
// return value > 1 ;
return value > 100 ;
})
console.log(res3);

运行结果:

04.png

5.findIndex

  • 语法形式:数组.findIndex(function( value , index , array ){ return 满足条件的数值; })
  • 参数:
    • 数组的每一项值
    • 数组的每一项实际的索引下标
    • 原始数组
  • 作用:查找满足条件的数值的第一次出现的索引下标;
  • 返回值:在数组内找到的第一个数据出现的下标 如果没有返回undefined.
const arr = [ 1 , 2 , 3 , 4 , 1 , 2 , 37 , 9, 5 , 6 ];
const res4 = arr.findIndex(function(value , index , array){
    // return value > 3;
    return value === 1 ;
})
console.log(res4);

运行结果:

05.png

6.every

  • 语法形式:数组.every(function( value , index , array ){ return 满足条件的数数组; })
  • 参数:
    • 数组的第一项实际的值
    • 数组的第一项实际的值对应的下标
    • 原始数组
  • 作用:判断数组内数据是否全部满足条件,全部都要满足条件
  • 返回值:返回布尔类型true/false;满足条件,返回true;不满足条件,返回false。
const arr = [ 1 , 2 , 3 , 4 , 1 , 2 , 37 , 9, 5 , 6 ];
const res5 = arr.every( function( value , index , array ){
    // return index === 30 ;
    return index > -1 ;
} )
console.log(res5);

运行结果:

06.png

7.some

  • 语法形式:数组.some(function( value , index , array ){ return 满足条件的数组; })
  • 参数:
    • 数组的每一项实际的值
    • 数组的每一项实际的值对应的下标
    • 原始数组
  • 作用:判断数组内数据是否有一项满足条件,有一项满足条件就可以
  • 返回值:返回布尔类型true/false;满足条件,返回true;不满足条件,返回false。
const arr = [ 1 , 2 , 3 , 4 , 1 , 2 , 37 , 9, 5 , 6 ];
const res6 = arr.some( function(value , index , array ){
    // return value > 8 ;
    return value < 0
} )
console.log(res6);

运行结果: 07.png

8.reduce

  • 语法形式:
    • 语法形式1:数组.reduce(function( prev , value , index , array ){ return 满足条件的数组; })
    • 语法形式2:数组.reduce(function( prev , value , index , array ){ return 满足条件的数组; },初始值)
  • 参数:
    • 表示初始值或者数组第一项的值
    • 数组每一项实际的值
    • 数组每一项实际的值对应的下标
    • 原始数组
  • 作用:累加(叠加);
  • 返回值:返回的是return操作后的具体数据数值.
const arr = [ 1 , 2 , 3 , 4 , 1 , 2 , 37 , 9, 5 , 6 ];
const res7 = arr.reduce( function( prev , value , index , array ){
      return prev ;
} )
console.log(res7);

const res8 = arr.reduce( function( prev , value , index , array ){
      return prev + '北京';
} )
console.log(res8);

运行结果:

01.png