其他分享
首页 > 其他分享> > 数组的一些常用方法分析 介绍

数组的一些常用方法分析 介绍

作者:互联网

reduce

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
function(total,currentValue, index,arr)	必需。用于执行每个数组元素的函数。
函数参数:
参数	描述
total	必需。初始值, 或者计算结束后的返回值。
currentValue	必需。当前元素
currentIndex	可选。当前元素的索引
arr	可选。当前元素所属的数组对象。
initialValue	可选。传递给函数的初始值

map

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

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

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

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

语法array.map(function(currentValue,index,arr), thisValue)

参数	描述
function(currentValue, index,arr)必须。函数,数组中的每个元素都会执行这个函数
函数参数:
参数描述
currentValue 必须。当前元素的值
index	可选。当前元素的索引值
arr	可选。当前元素属于的数组对象
thisValue	可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue,或者传入 null、undefined,那么回调函数的 this 为全局对象。

返回值:

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

filter的定义和用法

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

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

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

语法

array.filter(function(currentValue,index,arr), thisValue)

参数 描述
function(currentValue, index,arr)

参数	描述
currentValue	必须。当前元素的值
index	        可选。当前元素的索引值
arr	            可选。当前元素属于的数组对象
thisValue	    可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"

for of

	for...of 语句创建一个循环来迭代可迭代的对象。
	在 ES6 中引入的 for...of 循环,以替代 for...in 和 forEach() ,并支持新的迭代协议。
	for...of 允许你遍历 Arrays(数组), Strings(字符串), Maps(映射), Sets(集合)等可迭代的数据结构等。

语法

for (variable of iterable) {
    statement
}
variable:每个迭代的属性值被分配给该变量。
iterable:一个具有可枚举属性并且可以迭代的对象。

标签:index,currentValue,thisValue,arr,元素,常用,介绍,数组
来源: https://blog.csdn.net/zzzzz111333/article/details/114899553