js 数组 map,filter,reduce
2018-04-01 00:00
525 查看
filter
过滤器,按照一定条件,过滤数组中的元素
Function is a predicate, to test each element of the array. Return
The current element being processed in the array.
The index of the current element being processed in the array.
The array
Optional. Value to use as
map
映射,将数组中的元素,按照一定的规则映射为新数组
Function that produces an element of the new Array, taking three arguments:
The current element being processed in the array.
The index of the current element being processed in the array.
The array
Value to use as
reduce
聚合,将数组按照一定的方式,聚合为一个结果,这个结果不仅仅只是一个数,可以是对象或数组!!!
Function to execute on each element in the array, taking four arguments:
The accumulator accumulates the callback's return values; it is the accumulated value previously returned in the last invocation of the callback, or
The current element being processed in the array.
The index of the current element being processed in the array. Starts at index 0, if an
The array
Value to use as the first argument to the first call of the
过滤器,按照一定条件,过滤数组中的元素
选择数组中值大于3的组合为新数组 a.filter((item) => item > 3)
Syntax
var newArray = arr.filter(callback[, thisArg])
Parameters
callback
Function is a predicate, to test each element of the array. Return
trueto keep the element,
falseotherwise, taking three arguments:
element
The current element being processed in the array.
indexOptional
The index of the current element being processed in the array.
arrayOptional
The array
filterwas called upon.
thisArgOptional
Optional. Value to use as
thiswhen executing
callback.
map
映射,将数组中的元素,按照一定的规则映射为新数组
新数组中的元素是a中的数的平方 a.map( (item) => item * item )
Syntax
var new_array = arr.map(function callback(currentValue[, index[, array]]) { // Return element for new_array }[, thisArg])
Parameters
callback
Function that produces an element of the new Array, taking three arguments:
currentValue
The current element being processed in the array.
indexOptional
The index of the current element being processed in the array.
arrayOptional
The array
mapwas called upon.
thisArgOptional
Value to use as
thiswhen executing
callback.
reduce
聚合,将数组按照一定的方式,聚合为一个结果,这个结果不仅仅只是一个数,可以是对象或数组!!!
对a中的数字求和 a.reduce( (pre, cur) => pre += cur, 0 )
将a中的数字连接成字符串 a.reduce( (pre, cur) => pre += cur, '' )
对于a中的元素,如果该元素的平方比4大,则将该数的平方放入新数组 只使用map或filter是做不到的 a.reduce( (pre, cur) => { let ret = cur * cur if (ret > 4) pre.push(ret) return pre }, [] )
统计a中每个数字出现的次数,返回一个对象 对象的键是a中的数字种类,值是该数出现的次数 a.reduce( (pre, cur) => { pre[cur] = pre[cur] ? ++pre[cur] : 1 return pre }, {} )
同样的功能,使用逗号表达式和三元表达式简化箭头函数 a.reduce( (pre, cur) => (pre[cur] ? pre[cur]++ : pre[cur] = 1, pre), {} )
Syntax
arr.reduce(callback[, initialValue])
Parameters
callback
Function to execute on each element in the array, taking four arguments:
accumulator
The accumulator accumulates the callback's return values; it is the accumulated value previously returned in the last invocation of the callback, or
initialValue, if supplied (see below).
currentValue
The current element being processed in the array.
currentIndexOptional
The index of the current element being processed in the array. Starts at index 0, if an
initialValueis provided, and at index 1 otherwise.
arrayOptional
The array
reduce()was called upon.
initialValueOptional
Value to use as the first argument to the first call of the
callback. If no initial value is supplied, the first element in the array will be used. Calling
reduce()on an empty array without an initial value is an error.
相关文章推荐
- Js数组的map,filter,reduce,every,some方法
- js数组中forEach/some/every/map/filter/reduce的区别
- js中数组过滤、遍历、迭代every、some、filter、map、forEach、reduce、reduceRight
- 常用数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- Js 数组——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- Js 数组方法:filter()、map()、some()、every()、forEach()、indexOf()、lastIndexOf()、
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- Js 数组——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()
- JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some
- C#数组的Map、Filter、Reduce操作
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- JS数组方法总结shift()、unshift()、pop()、push()、concat()、splice()、filter()、map()、some()、every()、forEach()等方法
- JS数组中every(),filter(),forEach(),map(),some()方法学习笔记!
- 一张图看懂JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- JavaScript数组常用方法: indexOf、filter、forEach、map、reduce使用实例
- Js 数组——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()
- Array数组(JS)之map与reduce方法
- Js 数组——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()