zoukankan      html  css  js  c++  java
  • 对数组每项进行处理、判断的几种方法

    除了常用的for和while循环外,还有其他几种函数可以快速的处理数组

    1、forEach()

    array.forEach(function(currentValue, index, arr), thisValue)
    返回值: undefined

    var arr = [10, 20];
    
    arr.forEach(function(value, index, array) { // value数组每一项的值,index索引
      console.log(array);  // [10, 20]
      console.log(this); // {name: 123}
    }, obj);

    2、map()

    array.map(function(currentValue,index,arr), thisValue)
    返回值: 一个新数组

    var arr = [4, 9];
    var arr2 = arr.map(Math.sqrt);
    console.log(arr, arr2); // [4, 9] [2, 3]

    3、filter()

    array.filter(function(currentValue,index,arr), thisValue)
    返回值: 新数组,包含了符合条件的所有元素

    arr = [1, 3, 5, 2];
    var arr2 = arr.filter(function(value) {
      if (value > 2) {
        return value;
      }
    });
    console.log(arr, arr2); // [1, 3, 5, 2] [3, 5]

    4、every()

    array.every(function(currentValue,index,arr), thisValue)
    返回值: 布尔值
    如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。

    5、some()

    array.some(function(currentValue,index,arr),thisValue)
    返回值: 布尔值
    如果数组中有元素满足条件返回 true,否则返回 false。

  • 相关阅读:
    MT【319】分段递推数列
    MT【318】分式不等式双代换
    Centos7环境变量
    VI快捷键
    Centos7 开机自动运行命令
    Centos7 编辑本地DNS解析配置文件
    Centos7修改主机名
    xadmin 自定义过滤器选项
    Centos7网卡配置文件
    Centos7 挂载
  • 原文地址:https://www.cnblogs.com/Zting00/p/7497638.html
Copyright © 2011-2022 走看看