var arr=[] var arr = new Array() var arr1 = ["1", "2", "3"] var arr2 = ["a", "b", "c"] var arrLike = [{ name: '123', age: '18' }, { name: 'abc', age: '12' }] //類數組
eg:數組
obj = {"1": 'value is 1', length: 2} a = Array.from(obj) //[undefined, "value is 1"]
Array.prototype.concat(arr1, arr2) //["1", "2", "3", "a", "b", "c"] arr1.concat(arr2) //["1", "2", "3", "a", "b", "c"]
Array.prototype.join(separator) //以separator(默認爲逗號)拼接爲字符串。 Array.prototype.toString() //把數組轉換爲字符串, 數組中的元素之間用逗號分隔。
eg:函數
arr1.join() //"1,2,3" arr1.join('') //"123" arr1.join('/') //"1/2/3" arr1.toString() //"1,2,3"
Array.prototype.fill(value,start,end) //value 爲填充值 //start 爲填充數組元素起點座標 //end 爲填充數組元素終點座標
eg:prototype
arr1.fill(0,1,2) // ["1", 0, "3"]
Array.isArray() Array.isArray(arr1) //true
Array.prototype.filter() Array.prototype.map()
eg:code
var words = [ { id: '1', name: '123' }, { id: '2', name: 'abc' } ] words.filter(d => d.id === '1') //[{id: "1" , name: "123"}] 返回結果爲true的值 arr1.map(x => x * 2) // [2,4,6] 返回全部項
Array.prototype.reverse() //位置顛倒 Array.prototype.sort()
eg:對象
arr1.reverse() //["3", 2, "1"] arr1.sort() //["1", "2", "3"]
Array.prototype.reduce()
eg:排序
const reducer = (accumulation,currentValue)=>{ accumulation + currentValue } arr1.reduce(reducer) // 1+2+3=6 arr1.reduce(reducer,4) //4+1+2+3=10
Array.prototype.some(callback) //執行callback函數,直到callback返回true var even = function(element){ return element % 2 === 0 } arr1.some(even) //true
Array.prototype.every(callback) //數組的全部元素是否都經過callback函數 function even(currentValue){ return currentValue < 5 } arr1.every(even)
Array.prototype.find(callback) //在數組中返回符合callback第一個元素的值 function even(value){ return even % 2 === 0 } arr1.find(even) // 2
Array.prototype.findIndex(callback) //返回數組中知足callback的第一個元素的索引。不然返回-1 function even(value){ return value % 2 === 0 } arr1.findIndex(even) // 1
Array.prototype.includes(searchElement) //是否包含seachElement arr1.includes(2) //true arr1.includes(0) //false
Array.prototype.pop() //刪除數組的最後一個元素,並返回改元素的值 arr1.pop() // 3 arr1=[1,2]
Array.prototype.push() //增長元素到數組末尾,返回增長的元素 arr1.push(4) // 4 arr1=[1, 2, 3, 4]
Array.prototype.shift() //刪除數組第一個元素,返回這個元素值 arr1.shift() // 1 arr1=[2,3]
Array.prototype.unshift() //增長元素到數組開頭 ,返回數組長度 arr1.unshift(0) //4 arr1=[0,1,2,3]
Array.prototype.slice(start,end) //返回[start,end]淺拷貝到一個新數組,原數組不會被修改 arr1.slice(0,2) //[1,2] arr1=[1,2,3]
Array.prototype.splice() //經過刪除現有元素或添加新元素來更改一個數組的內容,原數組會被修改 Array.prototype.slice(replace-index,replace-num,replace-value) //replace-num = 0 => inserts arr1.splice(1,0,0) //arr1=[1,0,2,3] //replace-num !== 0 => replace arr1.splice(1,1,4) //返回被替換的元素 =>2 arr1=[1,4,3]
Array.prototype.map(callback) arr1.map(val=>val*2) // [2, 4, 6]
Array.prototype.forEach(callback) arr1.forEach(elem =>{ console.log(elem) }) // 1 // 2 // 3
Array.prototype.entries() //返回一個新的Array Iterator對象,該對象包含數組中每一個索引的鍵/值對 arr1 = ['a', 'b', 'c'] for( let [index,elem] of (arr1).entries()){ console.log(index,elem) } // 0 "a" // 1 "b" // 2 "c"
Array.prototype.keys() //返回一個新的Array迭代器,它包含數組中每一個索引的鍵 for(let index of(arr1).keys()){ console.log(index) } // 0 // 1 // 2
Array.prototype.values() //返回一個新的Array迭代對象,包含數組每一個索引值 for(let elem of(arr1).values()){ console.log(elem) } // a // b // c