arrayObject.unshift(newelement1,newelement2,....,newelementX)數組
let arr = [1,2,3]
let len = arr.unshift(1)
console.log(len) // 4
console.log(arr) // [1, 1, 2, 3]
複製代碼
arrayObject.shift()bash
let arr = [1,2,3]
console.log(arr.shift()) // 1
console.log(arr) // [2, 3]
複製代碼
arrayObject.push(newelement1,newelement2,....,newelementX)函數
let arr = [1,2,3]
console.log(arr.push(1,2)) // 5
console.log(arr) // [1, 2, 3, 1, 2]
複製代碼
arrayObject.pop()ui
let arr = [1,2,3]
console.log(arr.pop()) // 3
console.log(arr) // [1, 2]
console.log([].pop()) // undefined
複製代碼
arrayObject.concat(arrayX,arrayX,......,arrayX)this
let arr1 = [1,2,3], arr2 = [4,5,6], arr3 = [7]
console.log(arr1.concat(arr2, arr3)) // [1, 2, 3, 4, 5, 6, 7]
console.log(arr1) // [1, 2, 3]
複製代碼
arrayObject.splice(index,howmany,item1,.....,itemX)spa
let arr1 = [1,2,3]
console.log(arr1.splice(1,2)) // [2, 3]
console.log(arr1) // [1]
console.log(arr1.splice(1,0,2,3)) // []
console.log(arr1) // [1, 2, 3]
複製代碼
arrayObject.slice(start,end)prototype
let arr1 = [1,2,3]
console.log(arr1.slice(1, 2)) // [2]
console.log(arr1.slice(1)) // [2, 3]
console.log(arr1) // [1, 2, 3]
複製代碼
arrayObject.sort(sortby)code
let arr1 = [2,1,3]
console.log(arr1.sort()) // [1, 2, 3]
console.log(arr1) // [1, 2, 3]
複製代碼
!注意對象
因爲sort()方法是分別調用元素的toString()方法,本質上是比較字符串,於是會出現如下狀況排序
let arr1 = [3,10,2]
console.log(arr1.sort()) // [10, 2, 3]
console.log(arr1) // [10, 2, 3]
複製代碼
這是由於'10'和'2'會先比較'1',於是10在最前,改進方法以下,編寫一個比較函數
let arr1 = [3,10,2]
console.log(arr1.sort(compare)) // [2, 3, 10]
console.log(arr1) // [2, 3, 10]
function compare(value1, value2) {
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
}
複製代碼
arrayObject.reverse()
let arr1 = [1, 2, 3]
console.log(arr1.reverse()) // [3, 2, 1]
console.log(arr1) // [3, 2, 1]
複製代碼
arrayObject.join(separator)
let arr1 = [1,2,3]
console.log(arr1.join()) // 1,2,3
console.log(arr1.join('!')) // 1!2!3
console.log(arr1) // [1, 2, 3]
複製代碼
array.forEach(function(currentValue, index, arr), thisValue)
let arr = [1,2,3], newArr = []
arr.forEach(item => newArr.push(++item))
console.log(arr) // [1, 2, 3]
console.log(newArr) // [2, 3, 4]
複製代碼
array.map(function(currentValue,index,arr), thisValue)
let arr = [1,2,3], newArr = []
newArr = arr.map(function (item, index, array) {
console.log(this) // {a: 1}
return ++item
}, {a: 1})
console.log(arr) // [1, 2, 3]
console.log(newArr) // [2, 3, 4]
複製代碼
array.filter(function(currentValue,index,arr), thisValue)
let arr = [1,2,3], newArr = []
newArr = arr.filter(item => item >=2)
console.log(arr) // [1, 2, 3]
console.log(newArr) // [2, 3]
複製代碼
array.every(function(currentValue,index,arr), thisValue)
let arr = [1,2,3], newArr = []
let isTrue = arr.every(item => item > 0)
console.log(arr) // [1, 2, 3]
console.log(isTrue) // true
複製代碼
let arr = [1, 2, 3]
Array.isArray(arr) // true
複製代碼
let arr = [1, 2, 3]
arr instanceof Array // true
複製代碼
let arr = [1, 2, 3]
arr.constructor === Array // true
arr.constructor // ƒ Array() { [native code] }
複製代碼
不一樣對象的constructor
function f () {}
console.log([].constructor) // ƒ Array() { [native code] }
console.log({}.constructor) // ƒ Object() { [native code] }
console.log(f.constructor) // ƒ Function() { [native code] }
console.log((123).constructor) // ƒ Number() { [native code] }
console.log('123'.constructor) // ƒ String() { [native code] }
console.log(true.constructor) // ƒ Boolean() { [native code] }
複製代碼
自己用法:
Array.map(function(item, index, arr){}, thisValue)
實現原理:
Array.prototype.map = function (fn, context) {
let newArray = []
for(let i=0;i<this.length;i++){
// 經過call()來綁定回調函數的上下文
newArray.push(fn.call(context, this[i], i, this))
}
return newArray
}
複製代碼
自己用法:
Array.filter(function(item, index ,array){}, thisValue)
實現原理:
Array.prototype.filter = function (fn, context) {
let newArray = []
for (let i=0;i<this.length;i++) {
if(fn.call(context, this[i], i, this)) {
newArray.push(this[i])
}
}
return newArray
}
複製代碼