01、push()
:将value添加到数组的最后,返回新数组的长度(改变原数组)
let a = [1, 2, 3, 4, 5]
let result = a.push(1)
console.log(result)
console.log(a)
result = a.push('a', 'b')
console.log(result)
console.log(a)
复制代码
02、unshift()
:添加元素到数组的开头,返回新数组的长度(改变原数组)
let a = [1, 2, 3, 4, 5]
let result = a.unshift(1)
console.log(result)
console.log(a)
result = a.unshift('a', 'b')
console.log(result)
console.log(a)
复制代码
03、pop()
:删除数组中最后一个元素,返回被删除的元素(改变原数组)
let a = [5]
let result = a.pop()
console.log(result)
console.log(a)
result = a.pop()
console.log(result)
console.log(a)
复制代码
04、shift()
:删除数组第一个元素,返回被删除的元素(改变原数组)
let a = [5]
let result = a.shift()
console.log(result)
console.log(a)
result = a.shift()
console.log(result)
console.log(a)
复制代码
05、join(value)
:将数组用value连接为字符串,返回被连接后的字符串(不改变原数组)
let a = [1, 2, 3, 4, 5]
let result = a.join()
console.log(result)
result = a.join('')
console.log(result)
result = a.join(',')
console.log(result)
result = a.join('&')
console.log(result)
let obj = {
toString: function () {
console.log('调用了toString()方法!')
return 'a'
},
toValue: function () {
console.log('调用了toValue()方法!')
return 'b'
}
}
result = a.join(obj)
console.log(result)
console.log(a)
console.log(result.split('a'))
复制代码
06、reverse()
:反转数组,返回反转后的新数组(改变原数组)
let a = [1, 2, ,3, ,4, 5]
let result = a.reverse()
console.log(result)
console.log(a)
a = [1, [2, 3], [4, 5]]
result = a.reverse()
console.log(result)
console.log(a)
const deepReverse = (array) => {
let temp = array.reverse()
temp.forEach(v => {
if (Object.prototype.toString.call(v) === '[object Array]') {
deepReverse(v)
}
})
return temp
}
a = [1, [2, 3], [4, 5]]
result = deepReverse(a)
console.log(result)
复制代码
07、slice(start, end)
:获取子数组,包含原数组索引start的值到索引end的值,不包含end,返回获取的子数组(不改变原数组)
let a = [1, 2, 3, 4, 5]
let result = a.slice(2, 4)
console.log(result)
console.log(a)
console.log(a.slice(1))
console.log(a.slice(-1))
console.log(a.slice(-1, 1))
console.log(a.slice(1, -1))
console.log(a.slice(-1, -2))
console.log(a.slice(-2, -1))
复制代码
08、splice(index, count, value1, value2....)
:从索引位index处删除count个元素,插入value1, value2等元素,返回被删除的元素组成的新数组(改变原数组)
let a = [1, 2, 3, 4, 5]
let result = a.splice(1, 2, 0)
console.log(result)
console.log(a)
a = [1, 2, 3, 4, 5]
console.log(a.splice(-2))
console.log(a)
a = [1, 2, 3, 4, 5]
console.log(a.splice(-1))
console.log(a)
a = [1, 2, 3, 4, 5]
console.log(a.splice(0))
console.log(a)
a = [1, 2, 3, 4, 5]
console.log(a.splice(1))
console.log(a)
a = [1, 2, 3, 4, 5]
console.log(a.splice(-1, 2))
console.log(a)
a = [1, 2, 3, 4, 5]
console.log(a.splice(0, 2, 'a', 'b', 'c'))
console.log(a)
复制代码
09、sort()
:对数组元素进行排序,返回排序后的新数组(改变原数组)
let a = [31, 22, 27, 1, 9]
let result = a.sort()
console.log(result)
console.log(a)
a = ['c', 'ac', 'ab', 'A1', '1c', 13, 12, '13', '12', '3', '2', '1b', '1a', 1, 'aa', 'a', 3, 'b', 2]
a.sort()
console.log(a)
a = [31, 22, 27, 1, 9]
a.sort((a, b) => {
return a - b
})
console.log(a)
a = [31, 22, 27, 1, 9]
a.sort((a, b) => {
return b - a
})
console.log(a)
复制代码
10、toString()
:将数组中的元素用逗号拼接成字符串,返回拼接后的字符串(不改变原数组)
let a = [1, 2, 3, 4, 5]
let result = a.toString()
console.log(result)
console.log(a)
result = String(a)
console.log(result)
复制代码
11、indexOf(value)
:从索引为0开始,检查数组中是否包含有value,有则返回匹配到的第一个索引,没有则返回-1(不改变原数组)
let a = [1, 2, 3, 4, 5]
let result = a.indexOf(2)
console.log(result)
console.log(a)
result = a.indexOf(6)
console.log(result)
console.log(a)
复制代码
12、lastIndexOf(value)
:从最后的索引开始,检查数组找那个是否包含value,有则返回匹配到的第一个索引,没有返回-1(不改变原数组)
let a = [1, 2, 3, 2, 5]
let result = a.lastIndexOf(2)
console.log(result)
console.log(a)
result = a.lastIndexOf(6)
console.log(result)
console.log(a)
复制代码
13、concat(value)
:将数组和(或)值连接成新数组,返回新数组(不改变原数组)
let a = [1, 2], b = [3, 4], c = 5
let result = a.concat(b, c)
console.log(result)
console.log(a)
b = [3, [4]]
result = a.concat(b, c)
console.log(result)
console.log(a)
复制代码
14、forEach()
:对数组进行遍历循环,对数组中每一项运行给定函数,参数都是function类型,默认有传参,参数分别为:遍历数组内容、对应的数组索引、数组本身。没有返回值
var arr = [1, 2, 3, 4, 5]
arr.forEach(function (item, index, a) {
console.log(item + '|' + index + '|' + (a === true))
})
复制代码
15、map()
:指“映射”,对数组中的每一项运行给定函数,返回每次函数调用的结果组成的新数组
var arr = [1, 2, 3, 4, 5]
var arr1 = arr.map(function (item, index, a) {
return item * item
})
console.log(arr1)
复制代码
16、filter()
:“过滤”功能,数组中的每一项运行给定函数,返回满足过滤条件组成的数组
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var arr1 = arr.filter(function (item, index, a) {
return index % 3 === 0 || item >= 8
})
console.log(arr1)
复制代码
17、every()
:判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回true
var arr = [1, 2, 3, 4, 5]
var arr1 = arr.every(function (item, index, a) {
return item < 10
})
console.log(arr1)
var arr2 = arr.every(function (item, index, a) {
return item < 3
})
console.log(arr2)
复制代码
18、some()
:判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true
var arr = [1, 2, 3, 4, 5]
var arr1 = arr.some(function (item, index, a) {
return item < 3
})
console.log(arr1)
var arr2 = arr.some(function (item, index, a) {
return item < 1
})
console.log(arr2)
复制代码
综上可知,改变原数组的API如下:
- push()、unshift()、pop()、shift()、reverse()、splice()、sort()