forEach()    map()    filter()    some()    every()   find()    findIndex()

一、forEach()

语法: arr.forEach(function(val, index,arr){},thisValue)

参数:

  val:  必须, 当前数组的每一项

  index: 可选, 当前数组的索引值

  arr: 当前元素所属的数组

thisValue: 可选。传递给函数的值一般用 "this" 值。如果这个参数为空, "undefined" 会传递给 "this" 值

遍历 arr 数组

var array1 = [
      { "id": 0, "info": "dsfffffffffffhd", "done": false },
      { "id": 1, "info": "dsfffffffffffhd", "done": true },
      { "id": 22, "info": "dsfffffffffffhd", "done": false },
      { "id": 3, "info": "dsfffffffffffhd", "done": true },
      { "id": 42, "info": "dsfffffffffffhd", "done": false },
      { "id": 53, "info": "dsfffffffffffhd", "done": false },
      { "id": 6, "info": "dsfffffffffffhd", "done": true }
    ]
    var arr1 = []
    var arr = array1.forEach((value, index) => {
      if (value.done === true) {
        arr1.push(value)  // 把符合条件的添加到 arr1 中
      }
    });
    console.log(arr1);

 


二、map()

数组.map(function(value,index,currentArray){return 操作}) 遍历数组中每一个元素,更改后存入一个新的数组中 返回新数组

 参数:  

  value: 当前元素的值

  index: 当前元素的索引值

  arr: 当前元素属于的数组对象

thisValue: 可选, 对象作为该执行回调时使用,传递给函数, 作用"this"的值, 如果省略了thisValue, 'this'的值为'undefined'

 

 返回值: 返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

定义和用法

          map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

          map() 方法按照原始数组元素顺序依次处理元素。

          注意: map() 不会对空数组进行检测。

          注意: map() 不会改变原始数组。

var array1 = [
      { "id": 0, "info": "dsfffffffffffhd", "done": false },
      { "id": 1, "info": "dsfffffffffffhd", "done": true },
      { "id": 22, "info": "dsfffffffffffhd", "done": false },
      { "id": 3, "info": "dsfffffffffffhd", "done": true },
      { "id": 42, "info": "dsfffffffffffhd", "done": false },
      { "id": 53, "info": "dsfffffffffffhd", "done": false },
      { "id": 6, "info": "dsfffffffffffhd", "done": true }
    ]
    var arr1 = []
    var arr = array1.map((value, index) => {
      if (value.done === true) {
        arr1.push(value) // 把符合条件的添加到 arr1中
      }
    });
    console.log(arr1);


 

 

 

 三、数组名.filter(function(value,index,currentArray){ return 条件})  过滤出符合条件的元素,返回一个新数组

参数: 

  value  必须。当前元素的值

  index 可选。当前元素的索引值

  currentArray 可选。当前元素属于的数组对象


四、some()

数组名.some(function(value,index,currentArray){return 条件}, thisValue) 验证数组中的每个元素是否都符合指定条件,返回布尔值 

参数: 

  value: 当前元素的值

  index: 当前元素的索引值

  arr: 当前元素属于的数组对象

thisValue: 可选, 对象作为该执行回调时使用,传递给函数, 作用"this"的值, 如果省略了thisValue, 'this'的值为'undefined'

注意: 

  some() 方法用于检测数组中的元素是否满足指定条件(函数提供)。

       some() 方法会依次执行数组的每个元素:

返回值:如果有一个元素满足条件,则表达式返回true, 剩余的元素不会再执行检测。

       如果没有满足条件的元素,则返回false。

       注意: some() 不会对空数组进行检测。

       注意: some() 不会改变原始数组。 


五、every()

数组名.every(function(value,index,currentArray){return 条件}) 验证数组中的每个元素是否都符合指定条件,返回布尔值

参数: 

  varlue: 当前元素的值

  index: 当前元素的索引

  arr: 当前元素属于的数组对象

thisValue: 可选, 对象作为该执行回调时使用,传递给函数, 作用"this"的值, 如果省略了thisValue, 'this'的值为'undefined'

注意: 

     every() 方法用于检测数组所有元素是否都符合指定条件(通过函数提供)。

          every() 方法使用指定函数检测数组中的所有元素:

          如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。

          如果所有元素都满足条件,则返回 true。

          注意: every() 不会对空数组进行检测。

          注意: every() 不会改变原始数组。


六、find()  犯得

array.find(function(currentValue, index, arr),thisValue) 返回符合测试条件的第一个数组元素值,如果没有符合条件的则返回 undefined。

参数: 

  currentValue 必需。当前元素

  index 可选。当前元素的索引值

  arr 可选。当前元素所属的数组对象

thisValue 可选。 传递给函数的值一般用 "this" 值。如果这个参数为空, "undefined" 会传递给 "this" 值

注意: 

find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

find() 方法为数组中的每个元素都调用一次函数执行:

当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。

如果没有符合条件的元素返回 undefined

注意: find() 对于空数组,函数是不会执行的。

注意: find() 并没有改变数组的原始值。


七、findIndex()

array.findIndex(function(currentValue, index, arr), thisValue) 返回符合测试条件的第一个数组元素索引,如果没有符合条件的则返回 -1。

参数:

  currentValue 必需。当前元素

  index 可选。当前元素的索引

  arr 可选。当前元素所属的数组对象

thisValue 可选。 传递给函数的值一般用 "this" 值。如果这个参数为空, "undefined" 会传递给 "this" 值

注意:

findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。

findIndex() 方法为数组中的每个元素都调用一次函数执行:

当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。

如果没有符合条件的元素返回 -1

注意: findIndex() 对于空数组,函数是不会执行的。

注意: findIndex() 并没有改变数组的原始值。


八、filter() 飞优特

array.filter(function(currentValue,index,arr), thisValue)   返回数组,包含了符合条件的所有元素。如果没有符合条件的元素则返回空数组。

参数: 

  currentValue 必须。当前元素的值

  index 可选。当前元素的索引值

  arr 可选。当前元素属于的数组对象

thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。

如果省略了 thisValue ,"this" 的值为 "undefined"

filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

注意: filter() 不会对空数组进行检测。

注意: filter() 不会改变原始数组。




 

android 多个byte数组拼接 byte数组遍历_数组元素

var arr = [
      { "id": 0, "info": "dsfffffffffffhd", "done": false },
      { "id": 1, "info": "dsfffffffffffhd", "done": true },
      { "id": 22, "info": "dsfffffffffffhd", "done": false },
      { "id": 3, "info": "dsfffffffffffhd", "done": true },
      { "id": 42, "info": "dsfffffffffffhd", "done": false },
      { "id": 53, "info": "dsfffffffffffhd", "done": false },
      { "id": 6, "info": "dsfffffffffffhd", "done": true }
    ]














数组遍历