一、數(shù)組的判斷
arr instance Array;
Array.isArray(arr);
方法:
1. push
作用:像數(shù)組的末尾添加一項(xiàng)或多項(xiàng)元素
參數(shù):要添加的項(xiàng)
返回值:新數(shù)組的長(zhǎng)度
是否改變?cè)瓟?shù)組:改變
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
作用:刪除數(shù)組的最后一項(xiàng)
參數(shù):無
返回值:被刪除的項(xiàng)
是否改變?cè)瓟?shù)組:改變
varary = ['1','2','3'];varres = ary.pop();console.log(ary);// ['1','2']console.log(res);// 3
3. shift
作用:刪除數(shù)組的首項(xiàng)
參數(shù):無
返回值:被刪除的項(xiàng)
是否改變?cè)瓟?shù)組:改變
varary = ['a','b','c'];varres = ary.shift();console.log(ary);// ['b','c']console.log(res);// a
4. unshift
作用:向數(shù)組的開頭添加一或多項(xiàng)
參數(shù):要添加的項(xiàng),多項(xiàng)用','隔開
返回值:新數(shù)組的長(zhǎng)度
是否改變?cè)瓟?shù)組:改變
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
作用:增刪改
參數(shù):ary.splice(index,howmany,item1,.....,itemX)
返回值:刪除的項(xiàng)
是否改變?cè)瓟?shù)組:改變
增加的功能
ary.splice(n,0,x,......,y);
從數(shù)組的索引n開始,刪除0項(xiàng),在索引n的前邊增加新的項(xiàng),第三個(gè)參數(shù)開始都是用來填補(bǔ)刪除的項(xiàng)目位置的
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項(xiàng),返回一個(gè)空數(shù)組
刪除的功能
ary.splice(n,m);
從數(shù)組的索引n開始,刪除m項(xiàng)
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);
從數(shù)組的索引n開始,刪除m項(xiàng),把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,新的項(xiàng)) //因?yàn)閟plice是在索引前添加,所以第一個(gè)參數(shù)為ary.length//模擬pop(尾部刪除)
ary.splice(arr.length-1,1);
//模擬shift(首項(xiàng)刪除)
ary.splice(0,1)
//模擬unshift(首項(xiàng)添加) 和unshilft二者返回值不同
ary.splice(0,0,新的項(xiàng))
此外
ary.splice(n)// 表示從索引n開始刪除到末尾ary.splice(0)// 刪除整個(gè)數(shù)組 有克隆數(shù)組的效果,利用返回值
6. slice
作用:截取數(shù)組(復(fù)制數(shù)組)
參數(shù):array.slice(start, end)
返回值:返回一個(gè)新數(shù)組
是否改變?cè)瓟?shù)組:不改變
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支持負(fù)參數(shù),從最后一項(xiàng)開始算起,-1為最后一項(xiàng),-2為倒數(shù)第二項(xiàng)slice(n)//從索引n開始復(fù)制到最后一項(xiàng)slice()、 slice(0)//復(fù)制整個(gè)數(shù)組
7. join
作用:用指定的分隔符將數(shù)組每一項(xiàng)拼接為字符串
參數(shù):指定的分隔符,如果省略該參數(shù),則使用逗號(hào)作為分隔符
返回值:拼接好的字符串
是否改變?cè)瓟?shù)組:不改變
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
作用:用于連接兩個(gè)或多個(gè)數(shù)組
參數(shù):參數(shù)可以是具體的值,也可以是數(shù)組對(duì)象??梢允侨我舛鄠€(gè)
返回值:返回連接后的新數(shù)組
是否改變?cè)瓟?shù)組:不改變
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() 如果操作的參數(shù)是數(shù)組,那么添加的是數(shù)組中的元素,而不是數(shù)組。 如果是二維(或以上)數(shù)組,concat只能'拆開'一層數(shù)組console.log(res4)// [1, 2, 3, 4, 5]? 如果concat()沒有參數(shù)或者參數(shù)是空數(shù)組也可以達(dá)到克隆數(shù)組的目的
9. sort
作用:對(duì)數(shù)組的元素進(jìn)行排序
參數(shù):可選(函數(shù)) 規(guī)定排序規(guī)則 默認(rèn)排序順序?yàn)榘醋帜干?/p>
返回值:排好序的原數(shù)組
是否改變?cè)瓟?shù)組:改變
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的參數(shù)函數(shù)總的形參a,b就是數(shù)組排序時(shí)候的相鄰比較的兩項(xiàng)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
作用:倒序數(shù)組
參數(shù):無
返回值:倒序后的原數(shù)組
是否改變?cè)瓟?shù)組:改變
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
作用:查找指定元素的位置
參數(shù):array.indexOf(item,start) item:查找的元素 start:字符串中開始檢索的位置
返回值:返回第一次查到的索引,未找到返回-1
是否改變?cè)瓟?shù)組: 不改變
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
作用:查找指定元素最后出現(xiàn)的位置
參數(shù):array.indexOf(item,start) item:查找的元素 start:字符串中開始檢索的位置
返回值:返回查到的元素的索引,未找到返回-1
是否改變?cè)瓟?shù)組:不改變
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
作用:循環(huán)遍歷數(shù)組每一項(xiàng)
參數(shù):函數(shù) ary.forEach(function(item,index,ary){}) item:每一項(xiàng) index:索引 ary:當(dāng)前數(shù)組
返回值:無
是否改變?cè)瓟?shù)組: 不改變
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
作用:數(shù)組中的元素為原始數(shù)組元素調(diào)用函數(shù)處理后的值
參數(shù):函數(shù) ary.map(function(item,index,ary){})item:每一項(xiàng)index:索引 ary:當(dāng)前數(shù)組
返回值:新數(shù)組
是否改變?cè)瓟?shù)組:不改變
varary = ['a','b','c']varres = ary.map(function(item,index,ary){returnitem+1;
})console.log(res)// ["a1", "b1", "c1"]
15. filter
作用:創(chuàng)建一個(gè)新的數(shù)組,新數(shù)組中的元素是通過檢查指定數(shù)組中符合條件的所有元素。
參數(shù):函數(shù) ary.filter(function(item,index,ary){})item:每一項(xiàng)index:索引 ary:當(dāng)前數(shù)組
返回值:新數(shù)組
是否改變?cè)瓟?shù)組:不改變
varary = [1,2,3,4,5,6]varres = ary.filter(function(item){returnitem<3;
})console.log(res)// [1,2]
16. every
作用:檢測(cè)數(shù)組所有元素是否都符合指定條件
參數(shù):函數(shù) ary.every(function(item,index,ary){})item:每一項(xiàng)index:索引 ary:當(dāng)前數(shù)組
返回值:布爾值
是否改變?cè)瓟?shù)組: 不改變
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 如果數(shù)組中檢測(cè)到有一個(gè)元素不滿足,則整個(gè)表達(dá)式返回false,且剩余的元素不會(huì)再進(jìn)行檢測(cè)。
2 如果所有元素都滿足條件,則返回true。
17. some
作用:檢測(cè)數(shù)組中的元素是否滿足指定條件
參數(shù):函數(shù) ary.some(function(item,index,ary){})item:每一項(xiàng)index:索引 ary:當(dāng)前數(shù)組
返回值:布爾值
是否改變?cè)瓟?shù)組:不改變
varary = [1,2,3,4,5,6]varres = ary.some(function(item){returnitem<3;
})console.log(res)// true;1 如果有一個(gè)元素滿足條件,則表達(dá)式返回true, 剩余的元素不會(huì)再執(zhí)行檢測(cè)。
2 如果沒有滿足條件的元素,則返回false。