0一、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)
複製代碼
0二、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)
複製代碼
0三、pop()
:刪除數組中最後一個元素,返回被刪除的元素(改變原數組)
let a = [5]
let result = a.pop()
console.log(result)
console.log(a)
result = a.pop()
console.log(result)
console.log(a)
複製代碼
0四、shift()
:刪除數組第一個元素,返回被刪除的元素(改變原數組)
let a = [5]
let result = a.shift()
console.log(result)
console.log(a)
result = a.shift()
console.log(result)
console.log(a)
複製代碼
0五、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'))
複製代碼
0六、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)
複製代碼
0七、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))
複製代碼
0八、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)
複製代碼
0九、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)
複製代碼
十、toString()
:將數組中的元素用逗號拼接成字符串,返回拼接後的字符串(不改變原數組)
let a = [1, 2, 3, 4, 5]
let result = a.toString()
console.log(result)
console.log(a)
result = String(a)
console.log(result)
複製代碼
十一、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)
複製代碼
十二、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)
複製代碼
1三、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)
複製代碼
1四、forEach()
:對數組進行遍歷循環,對數組中每一項運行給定函數,參數都是function類型,默認有傳參,參數分別爲:遍歷數組內容、對應的數組索引、數組自己。沒有返回值
var arr = [1, 2, 3, 4, 5]
arr.forEach(function (item, index, a) {
console.log(item + '|' + index + '|' + (a === true))
})
複製代碼
1五、map()
:指「映射」,對數組中的每一項運行給定函數,返回每次函數調用的結果組成的新數組
var arr = [1, 2, 3, 4, 5]
var arr1 = arr.map(function (item, index, a) {
return item * item
})
console.log(arr1)
複製代碼
1六、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)
複製代碼
1七、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)
複製代碼
1八、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()