Array对象api
2020-02-26 本文已影响0人
乐闻x
![](https://img.haomeiwen.com/i10174664/93fcdfc901bf97f4.png)
一、Array api列表
- concat
let arr = [1,2,3] arr.concat(3) (4) [1, 2, 3, 3] //参数 //无副作用
- copyWithin
//copyWithin() 方法用于从数组的指定位置拷贝元素到数组的另一个指定位置中。 //array.copyWithin(target, start, end) !!start end 可选,无则数组第一位 arr.copyWithin(1,2,3) (3) [1, 3, 3]
- fill
//fill() 方法用于将一个固定值替换数组的元素。 //array.fill(value, start, end) arr.fill(1,2) (3) [1, 2, 1]
- find
//找到满足条件的元素
// array.find(function(currentValue, index, arr),thisValue)
- findIndex
// 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。
//用法同 find
- indexOf
// string.indexOf(searchvalue,start) 待查找字符;开始下标
- lastIndexOf
//同indexOf,从后向前查询
- pop
//数组尾部取出一个元素
- push
//数组尾部添加一个元素
- reverse
// 反转数组
- shift
// 数组头部取出一个元素
- unshift
//数组头部添加一个元素
- slice
//返回从原数组中指定开始下标到结束下标之间的项组成的新 数组。
//slice()方法可以接受一或两个参数,即要返回项的起始和结束位置。
//在只有一个参数的情况下, slice()方法返回从该参数指定位置开始到当前数组末尾的所有项。
//如果有两个参数,该方法返回起始和结束位置之间的项——但不包括结束位置的项。
- splice
// splice() 方法用于添加或删除数组中的元素。
//有副作用
// array.splice(index,howmany,item1,.....,itemX) 下标,修改长度,替换元素
- sort
//arr.sort(function(a,b)=>{})
//数组排序,可以自定义排序方式。return -1 0 1 三个值之一
- includes
//判断是否包括元素值
// arr.includes(searchElement, fromIndex) 查找元素;开始下标
- join
//arrayObject.join(separator)
//数组元素连接成字符串
- keys
- entries
- values
- forEach
// 没有返回值,不能通过return打断循环。通过throw Error 打断循环。
- filter
// “过滤”功能,数组中的每一项运行给定函数,返回满足过滤条件组成的数组。
- flat
//数组扁平化,扁平数组,数组降维
[1, [2, [3, [4, 5]]]].flat(2)
=> [1, 2, 3, [4, 5]]
// 不管嵌套多少层
[1, [2, [3, [4, 5]]]].flat(Infinity)
=> [1, 2, 3, 4, 5]
//自动跳过空位
[1, [2, , 3]].flat()
=>[1, 2, 3]
- flatMap
// 参数1:遍历函数,该遍历函数可接受3个参数:当前元素、当前元素索引、原数组
// 参数2:指定遍历函数中 this 的指向
[2, 3, 4].flatMap((x) => [x, x * 2])
=> [2, 4, 3, 6, 4, 8]
- flatten
- map
// array.map(function(currentValue,index,arr), thisValue)
//循环操作
- every
// 判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回true。
- some
// 判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。
- reduce
// array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
- reduceRigth
// reduceRight() 方法的功能和 [reduce()](https://www.runoob.com/jsref/jsref-reduce.html) 功能是一样的,不同的是 reduceRight() 从数组的末尾向前将数组中的数组项做累加。
//注意: reduce() 对于空数组是不会执行回调函数的。
- toLocaleString
- toString
- Array.of()
// 将参数中所有值作为元素形成数组。
Array.of(1, 2, 3, 4)
=> [1, 2, 3, 4]
- Array.from()
// 将类数组对象或可迭代对象转化为数组。
// Array.from(arrayLike[, mapFn[, thisArg]])
// Array 转化 Set
Array.from(new Set())
// Array 转化 Map
Array.from(new Map())