Array

2021/11/4

# 类方法

# new

  • 接收的是单个数组项
let arr = new Array(1, 2, 3); // [1, 2, 3]
1

错误示范

let arr = new Array([1, 2, 3]); // [[1, 2, 3]]
1

# isArray

  • 判断是否是数组的最常用的方法
let a = [1, 2, 3];
console.log(Array.isArray(a)); // true
1
2

# from

Array.from()

  • Array.from()将伪数组对象iterator转换成数组

伪数组对象:拥有一个length和若干个索引的对象

  • 调用:Array.from(iterator[, mapFn, thisArg])
  • 入参:Iterator[, 回调一次数组的map, map的绑定this]
  • 返回:Array
  • tip:回调mapFn只有两个参数 (item, index) => {}
  • tip:使用thisArg时,mapFn就不能使用箭头函数

# of

  • Array.of()new Array()/Array() 相似,都是创建新数组
console.log(Array.of(1, 2, 3)); // [1, 2, 3]
console.log(new Array(1, 2, 3)); // [1, 2, 3]

// 区别
console.log(Array.of(3)); // [3]
console.log(new Array(3)); // [undefined, undefined, undefined]
1
2
3
4
5
6

# 增删改查

# pop

  • 作用:数组取出最后一项(改变原数组)
  • 调用:arr.pop()
  • 返回:删除的项
const arr = [1, 2, 3];
console.log(arr.pop()); // 3
console.log(arr); // [1, 2]
1
2
3

# push

  • 作用:数组末尾添加项
  • 调用:arr.push(item[, item...])
  • 传入:any[, any]
  • 返回:any
  • tip:返回的是添加后数组的最后一项
const arr = [1, 2, 3, 4];
console.log(arr.push(5, 6, 7)); // 7
console.log(arr); // [1, 2, 3, 4, 5, 6, 7]
1
2
3

# shift

  • 作用:删除数组第一个元素
  • 调用:arr.shift()
  • 返回:any
const arr = [1, 2, 3, 4];
console.log(arr.shift()); // 1
console.log(arr); // [2, 3, 4]
console.log([].shift()); // undefined
1
2
3
4

# unshift

  • 作用:向数组头添加项
  • 调用:arr.unshift(item[, item...])
  • 入参:any[, any...]
  • 返回:Number(length)
  • tip:插入的顺序就是入参顺序,返回的是插入后的长度

# splice

  • 作用:删除、插入数组(修改原数组)
  • 调用:arr.splice(from[, delNum, newItem...])
  • 入参:Number[, Number, any...]
  • 返回:array
  • tip:返回的是删除的数组
  • tip:支持负值索引

# slice

  • 作用:切割数组,返回新的数组
  • 调用:arr.slice(start, end)
  • 入参:number, number
  • 返回:Array(新数组,不改变原数组)
const arr = [1, 2, 3];
console.log(arr.slice()); // 复制数组:[1, 2, 3]
console.log(arr.slice(1)); // [2, 3]
console.log(arr.slice(1, -1)); // [2]

console.log(arr); // [1, 2, 3]
1
2
3
4
5
6

# includes

  • 作用:检查数组中是否包含指定的项,可以指定开始查找的位置
  • 调用:arr.includes(item[, start])
  • 入参:any[, Number]
  • 返回:boolean
const arr = [1, 2, 3, 4];
console.log(arr.includes(2)); // true
console.log(arr.includes(2, 2)); // false
1
2
3
  • 起始长度大于数组长度,直接返回false
const arr = [1, 1, 1, 1];
console.log(arr.includes(1, 4)); // false
1
2

# every

  • 作用:查看数组是否所有项都满足条件
  • 调用:arr.every((item, index, arr) => boolean, thisArg)
  • 入参:Function[, Object]
  • 返回:Boolean
  • tip:要使用this就不能使用箭头函数

# some

  • 作用:判断数组中是否有符合要求的项
  • 调用:arr.some((item, index, arr) => bool, thisArg)
  • 入参:Function[, obj]
  • 返回:Boolean

# find

  • 作用:找到数组中第一个满足条件的项
  • 调用:arr.find((item, index, arr) => bool, thisArg)
  • 入参:Function[, Object]
  • 返回:any(数组中满足条件的项)
  • tip:使用thisArg就不能使用箭头函数

# findIndex

  • 作用:找到符合条件的第一项的下标
  • 调用:arr.findIndex((item, index, arr) => bool, thisArg)
  • 入参:Function[, Object]
  • 返回:Number
  • tip:使用thisArg就不能使用箭头函数

# indexOf

  • 作用:找到指定元素的下标,不存在则返回 -1
  • 调用:arr.indexOf(item[, start])
  • 入参:any[, Number]
  • 返回:Number

# lastIndexOf

  • 作用:反向查找指定元素的下标,可以指定末尾开始的下标
  • 调用:arr.lastIndexOf(item[, from])
  • 入参:any[, Number]
  • 返回:Number
  • tip:from支持负值,可以从后数
const arr = ['小张', '小张', '小李', '小张'];

console.log(arr.lastIndexOf('小张')); // 3
console.log(arr.lastIndexOf('小张', 2)); // 1
console.log(arr.lastIndexOf('小张', 0)); // 0
console.log(arr.lastIndexOf('小张', -2)); // 1

1
2
3
4
5
6
7

# at

支持的环境

  • 新方法,支持的环境较新
环境 版本
chrom 92
firefox 90
node 16.6

# 操作数组

# concat

  • 作用:拼接数组,自动打平一层入参,不改变原数组
  • 调用:arr1.concat(arr2[, arr3...])
  • 入参:any[, any...]
  • 返回:Array
  • tip:如果入参是数组,自动打平一层(flat)

# copyWithin

  • 作用:将原数组的目标index变为指定index区间的数,不会改变原数组长度
  • 调用:arr.copyWithin(index, start, end);
  • 入参:Number, Number, Number
  • 返回:改变后的原数组
  • tip:替换的长度根据(end - start)决定
const arr = [1, 2, 3, 4, 5];
const arr2 = [1, 2, 3, 4, 5];

console.log(arr.copyWithin(1, 3, 5)) // [1, 4, 5, 4, 5]
console.log(arr2.copyWithin(2, 1, 3)) // [1, 2, 2, 3, 5]
1
2
3
4
5

# fill

  • 作用:将数组填充,改变原数组
  • 调用:arr.fill(item[, start, end])
  • 入参:any(填充值)[, Number, Number]
  • 返回:Array
const arr = [1, 2, 3, 4, 5];

console.log(arr.fill(6, 2, 4)); // [1, 2, 6, 6, 5]
console.log(arr.fill(6)); // [6, 6, 6, 6, 6]
console.log(arr); // [6, 6, 6, 6, 6]
1
2
3
4
5

# join

  • 作用:将数组加入指定的分隔符转化成字符串
  • 调用:arr.join(str)
  • 入参:String
  • 返回:String
  • tip:入参不传默认是逗号
const arr = [1, '本书', true];
console.log(arr.join()); // 1,本书,true
console.log(arr.join('')); // 1本书true
1
2
3

split

  • 作用:将字符串反向转回数组 详情
const str = '1,本书,true';
console.log(str.split(',')); // ['1', '本书', 'true']
1
2

# reverse

  • 作用:反转数组,改变原数组
  • 调用:arr.reverse()
  • 返回:原数组引用
const arr = [1, 2, 3, 4, 5];
console.log(arr.reverse()); // [5, 4, 3, 2, 1]
console.log(arr); // [5, 4, 3, 2, 1]
1
2
3

# sort

  • 作用:排列数组,改变原数组
  • 调用:arr.sort((a, b) => Number)
  • 入参:conpareFn
  • 返回:原数组引用

conpareFn

  • 形式:(a, b) => Number
  • 返回值 > 0:a在后面
  • 返回值 < 0:a在前面

# flat

  • 作用:打平多维数组,不改变原数组
  • 调用:arr.flat(deepth)
  • 入参:Number(可选,默认是1)
  • 返回:Array
  • tip:可入参infinity直接无限打平成一维数组

# toString

  • 作用:将数组转化成字符串,以逗号分隔,不改变原数组
  • 调用:arr.toString()
  • 返回:String
const arr = [1, 2, true, '黄']
console.log(arr.toString()); // 1,2,true,黄
console.log(arr); // [1, 2, true, '黄']
1
2
3

# 高级方法

# filter

  • 作用:指定条件筛选数组项,返回新数组,不改变原数组
  • 调用:arr.filter((item, index, arr) => bool, thisArg)
  • 入参:Function[, Object]
  • 返回:Array
  • tip:使用thisArg不能使用箭头函数

# forEach

  • 作用:以迭代器的形式遍历数组
  • 调用:arr.forEach((item, index, arr) => {}, thisArg)
  • 入参:function[, obj]
  • 返回:undefined
  • tip:forEach无法中断,且遍历项不会中途改变遍历的项

# map

  • 作用:将数组执行一个处理函数,返回新的数组
  • 调用:arr.map((item, index, arr) => newItem, thisArg)
  • 入参:function[, obj]
  • 返回:Array

# reduce

reduce

  • 作用:遍历数组,获得一个迭代出来的结果
  • 调用:arr.reduce((pre, item, index, arr) => {}, pre)
  • 入参:function, any
  • 返回:any

# reduceRight

  • 作用:从右往左迭代数组,得出迭代结果
  • 调用:arr.reduceRight((pre, item, index, arr) => pre, pre)
  • 入参:Function, any
  • 返回:any
const arr = [1, 2, 3, 4];

console.log(arr.reduce((pre, item, index, arr) => pre + item, '')); // 1234
console.log(arr.reduceRight((pre, item, index, arr) => pre + item, '')); // 4321
1
2
3
4

# 迭代器

# keys

  • 作用:拿到所有项的键值(迭代器的形式)
  • 调用:arr.keys()
  • 返回:Iterator
  • tip:和Object.keys(arr) 有区别
const arr = [1, 2, ,4];
console.log(arr.keys().next().value); // 0
console.log([...arr.keys()]); // [0, 1, 2, 3]

// Object.keys(arr)会忽略掉没有值的keys
console.log(Object.keys(arr)); // ['0', '1', '3']
1
2
3
4
5
6

# entries

  • 作用:将数组以迭代器的形式返回
  • 调用:arr.entries()
  • 返回:数组的迭代器
const arr = [1, 2, 3, 4];
let arrEts = arr.entries();

console.log(arrEts.next()); // { value: [ 0, 1 ], done: false }
1
2
3
4
  • 可以用作for..of循环
const arr = [1, 2, 3, 4];
let arrEts = arr.entries();

for( let [index, value] of arrEts) {
    console.log(value); // 1 2 3 4
}
1
2
3
4
5
6

# values

  • 作用:拿到所有的值,以迭代器方式返回
  • 调用:arr.values()
  • 返回:Iterator
const arr = [1, 10, 100, 1000];
console.log(arr.values().next().value); // 1
1
2

# Symbol.iterator

  • 作用:将数组返回成一个迭代器
  • 调用:arr[ Symbol.iterator ] ()
  • 返回:Iterator
  • tip:默认情况下,实际效果和values() 一样
const arr = [1, 10, 100];
const arrItt = arr[Symbol.iterator]();
const values = arr.values();

console.log(arrItt.next().value); // 1
console.log(values.next().value); // 1
1
2
3
4
5
6
上次更新: 9/17/2024