Array 数组

[] 字面量

下标从0开始

1
2
3
let a = []

let b = [1, 2, 3, 'hahaha']

length 属性

长度

1
2
let a = [1, 2, 3];
a.length

method

.push() 数个元素增加到末尾,并返回添加后的长度
unshift() 数个元素增加到开头,并返回添加后的长度

.pop() 删除最后一个,并返回删除的
.shift() 删除第一个,并返回删除的

splice(下标, 个数) 增加、删除、替换

1
2
var fruits = ["A", "B", "C", "D"];
fruits.splice(2, 0, "X", "Y"); // A,B,X,Y,C,D

forEach()

1
2
3
4
5
6
7
const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// Expected output: "a"
// Expected output: "b"
// Expected output: "c"
1
forEach(callbackFn)

callbackFn (element, index, array) => {}
为数组中每个元素执行的函数。并会丢弃它的返回值。
该函数被调用时将传入以下参数:

element
数组中正在处理的当前元素。

index
数组中正在处理的当前元素的索引。

array
调用了 forEach() 的数组本身。

1
forEach(callbackFn, thisArg)

thisArg 可选
执行 callbackFn 时用作 this 的值。

Array.prototype.slice.call()

Array.prototype.slice()

call() 改变this指向。

call() 方法的作用和 apply() 方法类似,区别就是 call() 方法接受的是参数列表,而 apply() 方法接受的是一个参数数组。