数组的方法

63 阅读3分钟

foreach数组遍历

    参数是一个回调函数,数组遍历时,循环一次,该函数会被执行一次。回调函数有三个参数,分别是value,index,array;value是这次循环获取的元素,index是元素的索引,array是数组本身,回调函数没有返回值
    参数名为回调函数
    forEach没有返回值
   var sum = ""
        names.forEach(function (value, index, array) {
            console.log(value, index, array);
            sum += value;
        });
        console.log(sum);


foreach的实现,类似如下:
 function forEach(cb) {
            for (var i = 0, len = names.length; i < len; i++) {
                cb(names[i], i, names);
            }
        }

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

map:映射

映射:数据之间基于某种关系的一一对应。例如,[1,2,3,4] [10,20,30,40],前面数组中每个元素扩大10倍就是后面数组的对应元素,y=2*X+1,x扩大两倍再加1,就得到y 参数:回调函数。回调函数有返回值,需要返回,基于映射关系生成的新元素 数组映射;基于映射关系,得到一个新数组,新数组中元素与原数组中元素意义对应。 返回值:新元素构成的新数组。

 var result = names.map(function (value, index, array) {
          // console.log(value,index,array);
          return value[0];
      });
      console.log(result);


//以下为练习
var stus = [
          {
              id: 1,
              name: "张三",
              age: 19
          },
          {
              id: 1,
              name: "李四",
              age: 19
          }
      ]
      var result = stus.map(function (value, index, array) {
          return value.name;
      });
      console.log(result);

reduce数组聚合

reduce:减少,previous:以前的;current 数组聚合,将数据中多个数据,基于某种算法,得到一个结果值。 参数1:回调函数。回调函数有四个参数,分别是previousValue(回调函数上一次调用的返回值),currentValue(当前值,这次循环得到的元素),currentIndex(元素的索引),array(数组本身); 回调函数有返回值,返回这一次的聚合结果。 参数2:聚合的初始值。 返回值,聚合的结果

  var nums = [12, 23, 1, 20, 34, 56];
       var result1 = nums.reduce(function (previousValue, currentValue, currentIndex, array) {
           console.log(previousValue, currentValue, currentIndex, array);
           return previousValue + currentValue;
       }, 0);
       console.log(result1);

//以下为练习
//统计数组中每一个元素出现的次数,用数组的reduce方法实现
       var strs = ["h", "l", "e", "l", "o", "w", "o", "r", "l", "d"]
       var sul1 = strs.reduce(function (previousValue, currentValue, currentIndex, array) {
           // console.log(previousValue, currentValue, currentIndex, array);
           // previousValue[currentValue] 上一次返回值是否包含当前元素。
           if (previousValue[currentValue]) {
               previousValue[currentValue]++;
           } else {
               previousValue[currentValue] = 1;
           }
           return previousValue;
       }, {})
       console.log(sul1);

find

find:发现、找 找到数组中第一个满足条件的元素并返回。 参数:回调函数,需要有返回值,返回true(满足条件)或者false(不满足条件) 回调函数执行次数不确定,找到元素就结束循环。找不到元素就返回undefined 找到下列数组第一次出现的偶数并返回。

 var nums = [1, 23, 34, 63, 23, 67, 34, 56, 786]
        var res2 = nums.find(function (value, index, array) {
            console.log(index);//找到满足条件元素停止执行
            if (value % 2 == 0) {
                return true;
            }
            return false;
        });
        console.log(res2);


//以下为练习
 var stus1 = [
            {
                id: 1,
                name: "张三",
                age: 19
            },
            {
                id: 1,
                name: "李四",
                age: 32
            },
            {
                id: 1,
                name: "王二",
                age: 22
            },
            {
                id: 1,
                name: "麻子",
                age: 17
            }
        ]
        var xj = stus1.find(function (a, b, c) {
            if (a.age >= 20) {
                return true;
            }
            return false;
        })
        console.log(xj.name);

findindex

findindex:找到数组中第一个满足条件的元素并返回元素的索引。 参数:回调函数,需要有返回值,返回true(满足条件)或者false(不满足条件) 回调函数执行次数不确定,找到元素就结束循环。找不到元素就返回undefined 返回值:第一个满足条件的索引,找不到返回-1

 var nums = [1, 23, 34, 63, 23, 67, 34, 56, 786];
        var res3 = nums.findIndex(function (value, index, array) {
            if (value % 2 == 0) {
                return true;
            }
            return false;
        });
        console.log(res3);

filter

filter:过滤。 找到所有满足条件的元素 参数:回调函数,需要有返回值,返回true(满足条件)或者false(不满足条件) 回调函数执行次数lenght次 返回值:满足条件的元素构成的数组。

 var nums = [1, 23, 34, 63, 23, 67, 34, 56, 786];
       var res4 = nums.filter(function (value, index, array) {
           if (value % 2 == 0) {
               return true;
           }
           return false;
       })
       console.log(res4);

shot

sort:排序 对数组元素排序,不传参的时候,是按照ASCII码表排序。 参数:回调函数;回调函数有两个参数,a,b;a是数组中一个元素,b是数组中另一个元素。 回调函数的作用:是定义排序规则,需要返回正数,0,负数(正负数字随意) 返回值,排序后的数组,直接把原数组修改

  var result = nums.sort(function (a, b) {
           // return a-b;升序
           //return b-a;降序
           if (a > b) {
               return 1;//升序
           }
           if (a == b) {
               return 0;//相等
           }
           return -1;
       });
       console.log(result);//降序


       var arr = [4, 3, 6, 5, 7, 2, 1];
       arr.sort();
       arr.sort(function (a, b) {
           return b - a;
       });
       console.log(arr);

every

every:每一个; 判断是否数组中每一个元素都满足条件。 参数:回调函数:回调函数有三个参数,分别是value,index,array;value是元素,index是元素的索引,array是数组本身;回调函数有返回值,则需要返回boolean类型,true表示该元素满足条件,反之,false。回调函数执行次数不确定。 返回值:boolean每一个元素都满足,反之false

       var result = nums.every(function (value, index, array) {
          console.log("-------------");
          return value % 2 == 0
      });
      console.log(result);

some

some:一些; 判断数组中,是否有元素满足要求。 参数:回调函数:回调函数有三个参数,分别是value,index,array;value是元素,index是元素的索引,array是数组本身;回调函数有返回值,则需要返回boolean类型,true表示该元素满足条件,反之,false。回调函数执行次数不确定。 返回值:boolean有,反之false

  var nums = [1, 23, 3, 10, 20, 30, 55, 40, 7, 50, 60, 70, 9, 80]
      var result = nums.some(function (value, index, array) {
          console.log("-------------");
          return value % 200 == 0
      });
      console.log(result);