一、數組的判斷
arr instance Array;
Array.isArray(arr);
方法:
1. push
作用:像數組的末尾添加一項或多項元素
參數:要添加的項
返回值:新數組的長度
是否改變原數組:改變
var ary = ['a','b','c'];
var res = ary.push('d','e');
console.log(ary);? // ["a","b","c","d","e"]
console.log(res);? // 5
2. pop
作用:刪除數組的最后一項
參數:無
返回值:被刪除的項
是否改變原數組:改變
varary = ['1','2','3'];varres = ary.pop();console.log(ary);// ['1','2']console.log(res);// 3
3. shift
作用:刪除數組的首項
參數:無
返回值:被刪除的項
是否改變原數組:改變
varary = ['a','b','c'];varres = ary.shift();console.log(ary);// ['b','c']console.log(res);// a
4. unshift
作用:向數組的開頭添加一或多項
參數:要添加的項,多項用','隔開
返回值:新數組的長度
是否改變原數組:改變
var ary = ['a','b','c'];
var res = ary.unshift('d','e');
console.log(ary);? // ["d","e","a","b","c"]
console.log(res);? // 5
5. splice
作用:增刪改
參數:ary.splice(index,howmany,item1,.....,itemX)
返回值:刪除的項
是否改變原數組:改變
增加的功能
ary.splice(n,0,x,......,y);
從數組的索引n開始,刪除0項,在索引n的前邊增加新的項,第三個參數開始都是用來填補刪除的項目位置的
varary = [1,2,3,4,5];varres = 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項
varary = [1,2,3,4,5];varres = ary.splice(1,2);console.log(ary);// [1,4,5]console.log(res);// [2,3]
修改的功能
ary.splice(n,m,x);
從數組的索引n開始,刪除m項,把x添加到索引n前邊
varary = [1,2,3,4,5];varres = 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)// 刪除整個數組 有克隆數組的效果,利用返回值
6. slice
作用:截取數組(復制數組)
參數:array.slice(start, end)
返回值:返回一個新數組
是否改變原數組:不改變
varary = [1,2,3,4,5];varres = ary.slice(1,3);varres2 = 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)//復制整個數組
7. join
作用:用指定的分隔符將數組每一項拼接為字符串
參數:指定的分隔符,如果省略該參數,則使用逗號作為分隔符
返回值:拼接好的字符串
是否改變原數組:不改變
varary = [1,2,3,4,5];varres = ary.join('-');console.log(ary);// [1, 2, 3, 4, 5]console.log(res);// 1-2-3-4-5
8. concat
作用:用于連接兩個或多個數組
參數:參數可以是具體的值,也可以是數組對象。可以是任意多個
返回值:返回連接后的新數組
是否改變原數組:不改變
varary = [1,2,3,4,5];varres = ary.concat(6,7);varres2 = ary.concat(6,[7,8]);varres3 = ary.concat(6,[7,[8,9]]);varres4 = 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()沒有參數或者參數是空數組也可以達到克隆數組的目的
9. sort
作用:對數組的元素進行排序
參數:可選(函數) 規定排序規則 默認排序順序為按字母升序
返回值:排好序的原數組
是否改變原數組:改變
varary = [1,5,7,9,12,24,56,87,92];varary2 = [1,5,7,9,12,24,56,87,92];varary3 = [1,5,7,9,12,24,56,87,92];varres = ary.sort();varres2 = ary2.sort(function(a,b){returna-b;
})varres3 = ary3.sort(function(a,b){returnb-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]
10. reverse
作用:倒序數組
參數:無
返回值:倒序后的原數組
是否改變原數組:改變
varary = [1,2,3,4,5];varres = ary.reverse();console.log(ary);// [5, 4, 3, 2, 1]console.log(res);// [5, 4, 3, 2, 1]
11. indexOf
作用:查找指定元素的位置
參數:array.indexOf(item,start) item:查找的元素 start:字符串中開始檢索的位置
返回值:返回第一次查到的索引,未找到返回-1
是否改變原數組: 不改變
varary = [1,2,3,4,5]varres = ary.indexOf(3);console.log(ary);// [1,2,3,4,5]console.log(res);// 2varary = ['a','b','c','d','c'];varres = ary.indexOf('c',3);console.log(res)// 4
12. lastIndexOf
作用:查找指定元素最后出現的位置
參數:array.indexOf(item,start) item:查找的元素 start:字符串中開始檢索的位置
返回值:返回查到的元素的索引,未找到返回-1
是否改變原數組:不改變
varary = ['a','b','c','d','c'];varres = ary.lastIndexOf('c',3);varres2 = ary.lastIndexOf('c',1);console.log(res);// 2console.log(res2);// -1
13. forEach
作用:循環遍歷數組每一項
參數:函數 ary.forEach(function(item,index,ary){}) item:每一項 index:索引 ary:當前數組
返回值:無
是否改變原數組: 不改變
varary = ['a','b','c']varres = 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"]
*/returnitem;
})console.log(res)// undefined? 無返回值
14. map
作用:數組中的元素為原始數組元素調用函數處理后的值
參數:函數 ary.map(function(item,index,ary){})item:每一項index:索引 ary:當前數組
返回值:新數組
是否改變原數組:不改變
varary = ['a','b','c']varres = ary.map(function(item,index,ary){returnitem+1;
})console.log(res)// ["a1", "b1", "c1"]
15. filter
作用:創建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素。
參數:函數 ary.filter(function(item,index,ary){})item:每一項index:索引 ary:當前數組
返回值:新數組
是否改變原數組:不改變
varary = [1,2,3,4,5,6]varres = ary.filter(function(item){returnitem<3;
})console.log(res)// [1,2]
16. every
作用:檢測數組所有元素是否都符合指定條件
參數:函數 ary.every(function(item,index,ary){})item:每一項index:索引 ary:當前數組
返回值:布爾值
是否改變原數組: 不改變
varary = [1,2,3,4,5,6]varres = ary.every(function(item){returnitem<3;
})varres2 = ary.every(function(item){returnitem<7;
})console.log(res)// false;console.log(res2)// true;1 如果數組中檢測到有一個元素不滿足,則整個表達式返回false,且剩余的元素不會再進行檢測。
2 如果所有元素都滿足條件,則返回true。
17. some
作用:檢測數組中的元素是否滿足指定條件
參數:函數 ary.some(function(item,index,ary){})item:每一項index:索引 ary:當前數組
返回值:布爾值
是否改變原數組:不改變
varary = [1,2,3,4,5,6]varres = ary.some(function(item){returnitem<3;
})console.log(res)// true;1 如果有一個元素滿足條件,則表達式返回true, 剩余的元素不會再執行檢測。
2 如果沒有滿足條件的元素,則返回false。