- 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
- pop
作用:刪除數(shù)組的最后一項(xiàng)
參數(shù):無(wú)
返回值:被刪除的項(xiàng)
是否改變?cè)瓟?shù)組:改變
var ary = ['1','2','3'];
var res = ary.pop();
console.log(ary); // ['1','2']console.log(res); // 3
- shift
作用:刪除數(shù)組的首項(xiàng)
參數(shù):無(wú)
返回值:被刪除的項(xiàng)
是否改變?cè)瓟?shù)組:改變
var ary = ['a','b','c'];
var res = ary.shift();
console.log(ary); // ['b','c']console.log(res); // a
- unshift
作用:向數(shù)組的開(kāi)頭添加一或多項(xiàng)
參數(shù):要添加的項(xiàng)诱告,多項(xiàng)用','隔開(kāi)
返回值:新數(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
- splice
作用:增刪改
參數(shù):ary.splice(index,howmany,item1,.....,itemX)
返回值:刪除的項(xiàng)
是否改變?cè)瓟?shù)組:改變
增加的功能
ary.splice(n,0,x熬甫,......,y);
從數(shù)組的索引n開(kāi)始血公,刪除0項(xiàng),在索引n的前邊增加新的項(xiàng)耿眉,第三個(gè)參數(shù)開(kāi)始都是用來(lái)填補(bǔ)刪除的項(xiàng)目位置的
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項(xiàng)孝治,返回一個(gè)空數(shù)組
刪除的功能
ary.splice(n,m);
從數(shù)組的索引n開(kāi)始,刪除m項(xiàng)
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);
從數(shù)組的索引n開(kāi)始陶耍,刪除m項(xiàng)奋蔚,把x添加到索引n前邊
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,新的項(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開(kāi)始刪除到末尾ary.splice(0)
// 刪除整個(gè)數(shù)組 有克隆數(shù)組的效果泊碑,利用返回值
- slice
作用:截取數(shù)組(復(fù)制數(shù)組)
參數(shù):array.slice(start, end)
返回值:返回一個(gè)新數(shù)組
是否改變?cè)瓟?shù)組:不改變
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支持負(fù)參數(shù),從最后一項(xiàng)開(kāi)始算起毯欣,-1為最后一項(xiàng)馒过,-2為倒數(shù)第二項(xiàng)
slice(n) //從索引n開(kāi)始復(fù)制到最后一項(xiàng)
slice()、 slice(0) //復(fù)制整個(gè)數(shù)組
- join
作用:用指定的分隔符將數(shù)組每一項(xiàng)拼接為字符串
參數(shù):指定的分隔符,如果省略該參數(shù)酗钞,則使用逗號(hào)作為分隔符
返回值:拼接好的字符串
是否改變?cè)瓟?shù)組:不改變
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
- concat
作用:用于連接兩個(gè)或多個(gè)數(shù)組
參數(shù):參數(shù)可以是具體的值腹忽,也可以是數(shù)組對(duì)象⊙庾鳎可以是任意多個(gè)
返回值:返回連接后的新數(shù)組
是否改變?cè)瓟?shù)組:不改變
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()
//如果操作的參數(shù)是數(shù)組窘奏,那么添加的是數(shù)組中的元素,而不是數(shù)組葫录。
//如果是二維(或以上)數(shù)組着裹,concat只能'拆開(kāi)'一層數(shù)組console.log(res4)
// [1, 2, 3, 4, 5] 如果concat()沒(méi)有參數(shù)或者參數(shù)是空數(shù)組也可以達(dá)到克隆數(shù)組的目的
- sort
作用:對(duì)數(shù)組的元素進(jìn)行排序
參數(shù):可選(函數(shù)) 規(guī)定排序規(guī)則 默認(rèn)排序順序?yàn)榘醋帜干?br> 返回值:排好序的原數(shù)組
是否改變?cè)瓟?shù)組:改變
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的參數(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]
- reverse
作用:倒序數(shù)組
參數(shù):無(wú)
返回值:倒序后的原數(shù)組
是否改變?cè)瓟?shù)組:改變
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]
- indexOf
作用:查找指定元素的位置
參數(shù):array.indexOf(item,start) item:查找的元素 start:字符串中開(kāi)始檢索的位置
返回值:返回第一次查到的索引,未找到返回-1
是否改變?cè)瓟?shù)組: 不改變
var ary = [1,2,3,4,5]var res = ary.indexOf(3);
console.log(ary); // [1,2,3,4,5]console.log(res);
// 2var ary = ['a','b','c','d','c'];var res = ary.indexOf('c',3);console.log(res) // 4
- lastIndexOf
作用:查找指定元素最后出現(xiàn)的位置
參數(shù):array.indexOf(item,start) item:查找的元素 start:字符串中開(kāi)始檢索的位置
返回值:返回查到的元素的索引米同,未找到返回-1
是否改變?cè)瓟?shù)組:不改變
var ary = ['a','b','c','d','c'];
var res = ary.lastIndexOf('c',3);
var res2 = ary.lastIndexOf('c',1);console.log(res); // 2console.log(res2); // -1
- forEach
作用:循環(huán)遍歷數(shù)組每一項(xiàng)
參數(shù):函數(shù) ary.forEach(function(item,index,ary){}) item:每一項(xiàng) index:索引 ary:當(dāng)前數(shù)組
返回值:無(wú)
是否改變?cè)瓟?shù)組: 不改變
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 無(wú)返回值
- 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ù)組:不改變
var ary = ['a','b','c']var res = ary.map (function (item,index,ary) {
return item+1;
})console.log (res) // ["a1", "b1", "c1"]
- filter
作用:創(chuàng)建一個(gè)新的數(shù)組骇扇,新數(shù)組中的元素是通過(guò)檢查指定數(shù)組中符合條件的所有元素。
參數(shù):函數(shù) ary.filter(function(item,index,ary){}) item:每一項(xiàng) index:索引 ary:當(dāng)前數(shù)組
返回值:新數(shù)組
是否改變?cè)瓟?shù)組:不改變
var ary = [1,2,3,4,5,6]var res = ary.filter (function(item) {
return item<3;
}) console.log (res) // [1,2]
- every
作用:檢測(cè)數(shù)組所有元素是否都符合指定條件
參數(shù):函數(shù) ary.every(function(item,index,ary){}) item:每一項(xiàng) index:索引 ary:當(dāng)前數(shù)組
返回值:布爾值
是否改變?cè)瓟?shù)組: 不改變
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 如果數(shù)組中檢測(cè)到有一個(gè)元素不滿(mǎn)足窍霞,則整個(gè)表達(dá)式返回 false 。
//且剩余的元素不會(huì)再進(jìn)行檢測(cè)拯坟。2 如果所有元素都滿(mǎn)足條件但金,則返回 true。
- some
作用:檢測(cè)數(shù)組中的元素是否滿(mǎn)足指定條件
參數(shù):函數(shù) ary.some(function(item,index,ary){}) item:每一項(xiàng) index:索引 ary:當(dāng)前數(shù)組
返回值:布爾值
是否改變?cè)瓟?shù)組:不改變
var ary = [1,2,3,4,5,6]var res = ary.some (function(item) {
return item<3;
}) console.log(res)
// true;1 如果有一個(gè)元素滿(mǎn)足條件郁季,則表達(dá)式返回 true 冷溃。
//剩余的元素不會(huì)再執(zhí)行檢測(cè)钱磅。2 如果沒(méi)有滿(mǎn)足條件的元素,則返回 false似枕。