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。

  • 相关阅读:
    多选按钮CheckBox
    DatePicker和DataPickerDialog以及AutoCompleteTextView的基本使用方法
    Broadcast机制(二)
    广播机制(一)
    XML文件解析
    WIFI网络操作
    SQL Server Profiler工具
    SQL Server执行计划的理解
    SQL Server常用元数据函数
    SQL Server数学函数
  • 原文地址:https://www.cnblogs.com/Zting00/p/7497638.html
Copyright © 2011-2022 走看看