做用:像數組的末尾添加一項或多項元素 參數:要添加的項 返回值:新數組的長度 是否改變原數組:改變
var ary = ['a','b','c']; var res = ary.push('d','e'); console.log(ary); // ["a", "b", "c", "d", "e"] console.log(res); // 5
做用:刪除數組的最後一項 參數:無 返回值:被刪除的項 是否改變原數組:改變
var ary = ['1','2','3']; var res = ary.pop(); console.log(ary); // ['1','2'] console.log(res); // 3
做用:刪除數組的首項 參數:無 返回值:被刪除的項 是否改變原數組:改變
var ary = ['a','b','c']; var res = ary.shift(); console.log(ary); // ['b','c'] console.log(res); // a
做用:向數組的開頭添加一或多項 參數:要添加的項,多項用','隔開 返回值:新數組的長度 是否改變原數組:改變
var ary = ['a','b','c']; var res = ary.unshift('d','e'); console.log(ary); // ["d", "e", "a", "b", "c"] console.log(res); // 5
做用:增刪改 參數:ary.splice(index,howmany,item1,.....,itemX) 返回值:刪除的項 是否改變原數組:改變
增長的功能
ary.splice(n,0,x,......,y);
從數組的索引n開始,刪除0項,在索引n的前邊增長新的項,第三個參數開始都是用來填補刪除的項目位置的數組
var ary = [1,2,3,4,5]; var res = ary.splice(1,0,6,7); console.log(ary); // [1, 6, 7, 2, 3, 4, 5] console.log(res); // [] 刪除0項,返回一個空數組
刪除的功能
ary.splice(n,m);
從數組的索引n開始,刪除m項函數
var ary = [1,2,3,4,5]; var res = ary.splice(1,2); console.log(ary); // [1,4,5] console.log(res); // [2,3]
修改的功能
ary.splice(n,m,x);
從數組的索引n開始,刪除m項,把x添加到索引n前邊code
var ary = [1,2,3,4,5]; var res = ary.splice(1,2,6,7); console.log(ary); // [1, 6, 7, 4, 5] console.log(res); // [2,3]
//模擬 push(尾部添加) 和push兩者返回值不一樣 ary.splice(ary.length,0,新的項) //由於splice是在索引前添加,因此第一個參數爲ary.length //模擬 pop(尾部刪除) ary.splice(arr.length-1,1); //模擬 shift(首項刪除) ary.splice(0,1) //模擬 unshift(首項添加) 和unshilft兩者返回值不一樣 ary.splice(0,0,新的項)
此外對象
ary.splice(n) // 表示從索引n開始刪除到末尾 ary.splice(0) // 刪除整個數組 有克隆數組的效果,利用返回值
做用:截取數組(複製數組) 參數:array.slice(start, end) 返回值:返回一個新數組 是否改變原數組:不改變
var ary = [1,2,3,4,5]; var res = ary.slice(1,3); var res2 = ary.slice(-3,-1) console.log(ary); // [1,2,3,4,5] console.log(res); // [2,3] console.log(res2) //[3,4] slice支持負參數,從最後一項開始算起,-1爲最後一項,-2爲倒數第二項 slice(n) //從索引n開始複製到最後一項 slice()、 slice(0) //複製整個數組
做用:用指定的分隔符將數組每一項拼接爲字符串 參數:指定的分隔符,若是省略該參數,則使用逗號做爲分隔符 返回值:拼接好的字符串 是否改變原數組:不改變
var ary = [1,2,3,4,5]; var res = ary.join('-'); console.log(ary); // [1, 2, 3, 4, 5] console.log(res); // 1-2-3-4-5
做用:用於鏈接兩個或多個數組 參數:參數能夠是具體的值,也能夠是數組對象。能夠是任意多個 返回值:返回鏈接後的新數組 是否改變原數組:不改變
var ary = [1,2,3,4,5]; var res = ary.concat(6,7); var res2 = ary.concat(6,[7,8]); var res3 = ary.concat(6,[7,[8,9]]); var res4 = ary.concat(); console.log(ary); // [1, 2, 3, 4, 5] console.log(res); // [1, 2, 3, 4, 5, 6, 7] console.log(res2); //[1, 2, 3, 4, 5, 6, 7, 8] console.log(res3) // [1, 2, 3, 4, 5, 6, 7, [8,9]] concat() 若是操做的參數是數組,那麼添加的是數組中的元素,而不是數組。 若是是二維(或以上)數組,concat只能'拆開'一層數組 console.log(res4) // [1, 2, 3, 4, 5] 若是concat()沒有參數或者參數是空數組也能夠達到克隆數組的目的
做用:對數組的元素進行排序 參數:可選(函數) 規定排序規則 默認排序順序爲按字母升序 返回值:排好序的原數組 是否改變原數組:改變
var ary = [1,5,7,9,12,24,56,87,92]; var ary2 = [1,5,7,9,12,24,56,87,92]; var ary3 = [1,5,7,9,12,24,56,87,92]; var res = ary.sort(); var res2 = ary2.sort(function(a,b){ return a-b; }) var res3 = ary3.sort(function(a,b){ return b-a; }) // sort的參數函數總的形參a,b就是數組排序時候的相鄰比較的兩項 console.log(res); // [1, 12, 24, 5, 56, 7, 87, 9, 92] console.log(res2); // [1, 5, 7, 9, 12, 24, 56, 87, 92] console.log(res3); // [92, 87, 56, 24, 12, 9, 7, 5, 1]
做用:倒序數組 參數:無 返回值:倒序後的原數組 是否改變原數組:改變
var ary = [1,2,3,4,5]; var res = ary.reverse(); console.log(ary); // [5, 4, 3, 2, 1] console.log(res); // [5, 4, 3, 2, 1]
做用:查找指定元素的位置 參數:array.indexOf(item,start) item:查找的元素 start:字符串中開始檢索的位置 返回值:返回第一次查到的索引,未找到返回-1 是否改變原數組: 不改變
var ary = [1,2,3,4,5] var res = ary.indexOf(3); console.log(ary); // [1,2,3,4,5] console.log(res); // 2 var ary = ['a','b','c','d','c']; var res = ary.indexOf('c',3); console.log(res) // 4
做用:查找指定元素最後出現的位置 參數:array.indexOf(item,start) item:查找的元素 start:字符串中開始檢索的位置 返回值:返回查到的元素的索引,未找到返回-1 是否改變原數組:不改變
var ary = ['a','b','c','d','c']; var res = ary.lastIndexOf('c',3); var res2 = ary.lastIndexOf('c',1); console.log(res); // 2 console.log(res2); // -1
做用:循環遍歷數組每一項 參數:函數 ary.forEach(function(item,index,ary){}) item:每一項 index:索引 ary:當前數組 返回值:無 是否改變原數組: 不改變
var ary = ['a','b','c'] var res = ary.forEach(function(item,index,ary){ console.log(item,index,ary); /* a 0 ["a", "b", "c"] b 1 ["a", "b", "c"] c 2 ["a", "b", "c"] */ return item; }) console.log(res) // undefined 無返回值
做用:數組中的元素爲原始數組元素調用函數處理後的值 參數:函數 ary.map(function(item,index,ary){}) item:每一項 index:索引 ary:當前數組 返回值:新數組 是否改變原數組:不改變
var ary = ['a','b','c'] var res = ary.map(function(item,index,ary){ return item+1; }) console.log(res) // ["a1", "b1", "c1"]
做用:建立一個新的數組,新數組中的元素是經過檢查指定數組中符合條件的全部元素。 參數:函數 ary.filter(function(item,index,ary){}) item:每一項 index:索引 ary:當前數組 返回值:新數組 是否改變原數組:不改變
var ary = [1,2,3,4,5,6] var res = ary.filter(function(item){ return item<3; }) console.log(res) // [1,2]
做用:檢測數組全部元素是否都符合指定條件 參數:函數 ary.every(function(item,index,ary){}) item:每一項 index:索引 ary:當前數組 返回值:布爾值 是否改變原數組: 不改變
var ary = [1,2,3,4,5,6] var res = ary.every(function(item){ return item<3; }) var res2 = ary.every(function(item){ return item<7; }) console.log(res) // false; console.log(res2) // true; 1 若是數組中檢測到有一個元素不知足,則整個表達式返回 false ,且剩餘的元素不會再進行檢測。 2 若是全部元素都知足條件,則返回 true。
做用:檢測數組中的元素是否知足指定條件 參數:函數 ary.some(function(item,index,ary){}) item:每一項 index:索引 ary:當前數組 返回值:布爾值 是否改變原數組:不改變
var ary = [1,2,3,4,5,6] var res = ary.some(function(item){ return item<3; }) console.log(res) // true; 1 若是有一個元素知足條件,則表達式返回 true , 剩餘的元素不會再執行檢測。 2 若是沒有知足條件的元素,則返回 false。