js中數(shù)組的方法非常的多,功能也很強(qiáng)大。今天來(lái)總結(jié)一下js中數(shù)組的方法:
首先創(chuàng)建一個(gè)數(shù)組,數(shù)組中可以包含各種類型的數(shù)據(jù):
var arr = [2,3,5,"hello",true,"world",[5,6,7],{"name":"lily","age":18},function(){console.log(1)}];
console.log(typeof arr); // object
console.log(arr.length); // 9
console.log(arr);// [2, 3, 5, "hello", true, "world", Array[3], Object, function]
一. 以下的7種方法會(huì)改變?cè)瓟?shù)組:
1.unshift 在第一位添加一位或者多位數(shù)據(jù),返回新數(shù)組的長(zhǎng)度,會(huì)改變?cè)瓟?shù)組
var arr1 = arr.unshift(9,10);
console.log(arr);// [9,10, 2, 3, 5, "hello", true, "world", Array[3], Object, function]
console.log(arr1); // 11
2.shift 刪除第一位,返回刪除的數(shù)據(jù),會(huì)改變?cè)瓟?shù)組
var arr2 = arr.shift();
console.log(arr);// [3, 5, "hello", true, "world", Array[3], Object, function]
console.log(arr2); // 2
3.push 在最后一位添加一位或者多位數(shù)據(jù),返回新數(shù)組的長(zhǎng)度,會(huì)改變?cè)瓟?shù)組
var arr3 = arr.push(9,10,11);
console.log(arr);// [2, 3, 5, "hello", true, "world", Array[3], Object, function,9,10,11]
console.log(arr3); // 12
4.pop 刪除最后一位,返回刪除的數(shù)據(jù),會(huì)改變?cè)瓟?shù)組
var arr4 = arr.pop();
console.log(arr);// [2,3, 5, "hello", true, "world", Array[3], Object]
console.log(arr4); // function(){console.log(1)}
5.splice 在索引為n的開(kāi)始(包括n)的往后將m個(gè)值替換為(k1,k2,k3...kk)刪除指定位置,并替換,返回刪除的數(shù)據(jù),會(huì)改變?cè)瓟?shù)組
var arr5 = arr.splice(1,3,"a","b");
console.log(arr);// [2, "a", "b", true, "world", Array[3], Object, function]
console.log(arr5); // [3, 5, "hello"]
6.sort 排序(字符規(guī)則),返回結(jié)果,會(huì)改變?cè)瓟?shù)組
(1)數(shù)值排序(升序)
var arr = [1,2,5,3,6];
var arr6 = arr.sort(function(a,b){return a-b});
console.log(arr);// [1, 2, 3, 5, 6]
console.log(arr6); // [1, 2, 3, 5, 6]
(2)字符排序
var arr = ["a","hello","world","zoom","lily","boy"];
var arr6 = arr.sort();
console.log(arr);// ["a", "boy", "hello", "lily", "world", "zoom"]
console.log(arr6); // ["a", "boy", "hello", "lily", "world", "zoom"]
7.reverse 反轉(zhuǎn)數(shù)組,返回結(jié)果,會(huì)改變?cè)瓟?shù)組
var arr7 = arr.reverse();
console.log(arr);// [function, Object, Array[3], "world", true, "hello", 5, 3, 2]
console.log(arr7); // [function, Object, Array[3], "world", true, "hello", 5, 3, 2]
二. 以下的14種方法不會(huì)改變?cè)瓟?shù)組:
var arr = [2,3,5,"hello",true,"world",[5,6,7],{"name":"lily","age":18},function(){console.log(1)}];
1.slice(n,m)
功能:截取指定位置[n,m)的數(shù)組,并返回
參數(shù):n表式開(kāi)始的索引位置,m表式結(jié)束的索引位置(不包括)
返回值:截取后的新數(shù)組
demo:
var arr1 = arr.slice(2,5);
console.log(arr);// [2,3,5,"hello",true,"world",[5,6,7],{"name":"lily","age":18},function(){console.log(1)}];
console.log(arr1); // [5,"hello",true]
2.concat(data1,data2,....)
功能:合并數(shù)組,并返回合并之后的數(shù)據(jù)
參數(shù):所有參數(shù)可選,要合并的數(shù)據(jù);data為數(shù)組時(shí),將數(shù)組中的數(shù)據(jù)合并到原數(shù)組;data為具體數(shù)據(jù)時(shí)直接添加到原數(shù)組尾部;參數(shù)省略時(shí)創(chuàng)建原數(shù)組的副本
返回值:合并的新數(shù)組
demo:
var arr2 = arr.concat();
console.log(arr); // 原數(shù)組
console.log(arr2); // [2,3,5,"hello",true,"world",[5,6,7],{"name":"lily","age":18},function(){console.log(1)}];
console.log(arr == arr2); // false
console.log(arr.concat(12,34)); // [2,3,5,"hello",true,"world",[5,6,7],{"name":"lily","age":18},function(){console.log(1)},12,34];
console.log(arr.concat([1,2],[[56,"he"],"and",{"gender":0,"major":"electric"}])); // [2,3,5,"hello",true,"world",[5,6,7],{"name":"lily","age":18},function(){console.log(1)},1,2,[56,"he"],"and",{"gender":0,"major":"electric"}];
3.join(str)
功能:根據(jù)指定分隔符將數(shù)組中的所有元素放入一個(gè)字符串,并返回這個(gè)字符串
參數(shù):參數(shù)可選,默認(rèn)不寫(xiě)將數(shù)組直接去掉括號(hào)成為字符串返回,否則以傳入的字符作為分隔符
返回值:拼接的字符串
demo:
var str31 = arr.join();
var str32 = arr.join("*");
console.log(arr); // 原數(shù)組
console.log(str31); // 2,3,5,"hello",true,"world",[5,6,7],{"name":"lily","age":18},function(){console.log(1)}
console.log(str32); // 2*3*5*hello*true*world*5,6,7*[object Object]*function(){console.log(1)}
4.toString()
功能:轉(zhuǎn)換成字符串,類似于沒(méi)有參數(shù)的join()。該方法會(huì)在數(shù)據(jù)發(fā)生隱式類型轉(zhuǎn)換時(shí)被自動(dòng)調(diào)用,如果手動(dòng)調(diào)用,就是直接轉(zhuǎn)為字符串
參數(shù):無(wú)
返回值:字符串
demo:
var str4 = arr.toString();
console.log(arr);// 原數(shù)組
console.log(str4); // 2,3,5,"hello",true,"world",[5,6,7],{"name":"lily","age":18},function(){console.log(1)}
5.valueOf()
功能:返回?cái)?shù)組的原始值(一般情況下其實(shí)就是數(shù)組自身),一般由js在后臺(tái)調(diào)用,并不顯式的出現(xiàn)在代碼中
參數(shù):無(wú)
返回值:原數(shù)組
demo:
var arr5 = arr.valueOf();
console.log(arr); // 原數(shù)組
console.log(arr5); // 原數(shù)組
console.log(arr === arr5); // true
6.indexOf(value, start)
功能:根據(jù)指定的數(shù)據(jù),從左向右,查詢?cè)跀?shù)組中出現(xiàn)的位置,如果不存在指定的數(shù)據(jù),返回-1。該方法是查詢方法,不會(huì)對(duì)數(shù)組產(chǎn)生改變
參數(shù):value為要查詢的數(shù)據(jù);start為可選,表示開(kāi)始查詢的位置,從前往后查詢;當(dāng)start為負(fù)數(shù)時(shí),還是從前往后查,返回的是正數(shù)索引;如果查詢不到value的存在,則方法返回-1
返回值:查詢到的索引位置,查詢不到返回-1
demo:
var index61 = arr.indexOf("hello");
var index62 = arr.indexOf("hello",3);
var index63 = arr.indexOf("hello",4);
var index64 = arr.indexOf("hello",-1);
var index65 = arr.indexOf("hello",-3);
var index66 = arr.indexOf("hello",-9);
console.log(arr);// 原數(shù)組
console.log(index61); // 3
console.log(index62); // 3
console.log(index63); // -1
console.log(index64); // -1
console.log(index65); // -1
console.log(index66); // 3
7.lastIndexOf(value, start)
功能:根據(jù)指定的數(shù)據(jù),從后往前查詢,查詢?cè)跀?shù)組中出現(xiàn)的位置,如果不存在指定的數(shù)據(jù),返回-1。
參數(shù):value為要查詢的數(shù)據(jù);start為可選,表示開(kāi)始查詢的位置,從索引位置往前查詢;start可以為負(fù)數(shù),從數(shù)組負(fù)數(shù)索引位置往前查詢,返回的是正數(shù)索引;如果查詢不到value的存在,則方法返回-1
demo:
var lastind71 = arr.lastIndexOf("hello");
var lastind72 = arr.lastIndexOf("hello",3);
var lastind73 = arr.lastIndexOf("hello",2);
var lastind74 = arr.lastIndexOf("hello",-1);
var lastind75 = arr.lastIndexOf("hello",-3);
var lastind76 = arr.lastIndexOf("hello",-9);
console.log(arr);// 原數(shù)組
console.log(lastind71); // 3
console.log(lastind72); // 3
console.log(lastind73); // -1
console.log(lastind74); // 3
console.log(lastind75); // 3
console.log(lastind76); // -1
8.forEach(function(value,index,self){})--forEach(callback)
功能:ES5新增方法,用來(lái)遍歷數(shù)組,該方法沒(méi)有返回值。forEach接收的回調(diào)函數(shù)會(huì)根據(jù)數(shù)組的每一項(xiàng)執(zhí)行,該回調(diào)函數(shù)默認(rèn)有三個(gè)參數(shù)
參數(shù):value為遍歷到的數(shù)組的數(shù)據(jù),index為對(duì)應(yīng)的索引,self為數(shù)組自身
返回值:無(wú)(undefined,因?yàn)榛卣{(diào)函數(shù)沒(méi)有return)
demo:
var a8 = arr.forEach(function(value,index,self){
console.log(value + "--" + index + "--" + (arr == self));
// 2--0--true
// 3--1--true
// 5--2--true
// hello--3--true
// true--4--true
// world--5--true
// 5,6,7--6--true
// [object Object]--7--true
// function(){console.log(1)}--8--true
});
console.log(arr);// 原數(shù)組
console.log(a8); // undefined
9.map(callback)
功能:1.同forEach功能;2.map的回調(diào)函數(shù)會(huì)將執(zhí)行結(jié)果返回,最后map將所有回調(diào)函數(shù)的返回值組成新數(shù)組返回
參數(shù):callback默認(rèn)有三個(gè)參數(shù),分別為value,index,self
返回值:新數(shù)組
demo(功能2):
var arr9 = arr.map(function(value,index,slef){
return "hello" + value;
}) ;
console.log(arr);// 原數(shù)組
console.log(arr9);// ["hello2", "hello3", "hello5", "hellohello", "hellotrue", "helloworld", "hello5,6,7", "hello[object Object]", "hellofunction(){console.log(1)}"]
10.filter
功能:1.同forEach功能;2.filter的回調(diào)函數(shù)需要返回布爾值,當(dāng)為true時(shí),將本次數(shù)組的數(shù)據(jù)返回給filter,最后filter將所有回調(diào)函數(shù)的返回值組成新數(shù)組返回(此功能可理解為“過(guò)濾”)。
參數(shù):callback默認(rèn)有三個(gè)參數(shù),分別為value,index,self
返回值:過(guò)濾的符合條件的數(shù)據(jù)組成的數(shù)組
demo(功能2):
var arr10 = arr.filter(function(value,index,self){
return typeof value == "string"; // 注意string要小寫(xiě)
});
console.log(arr);// 原數(shù)組
console.log(arr10); // ["hello","world"]
11.some(callback)
功能:判斷數(shù)組中是否存在滿足條件的項(xiàng),只要有一項(xiàng)滿足條件,就會(huì)返回true
參數(shù):some()接收一個(gè)回調(diào)函數(shù)作為參數(shù),這個(gè)回調(diào)函數(shù)需要有返回值,some(callback);callback默認(rèn)有三個(gè)參數(shù),分別為value,index,self
返回值:true/false
功能1:因?yàn)橐袛鄶?shù)組中的每一項(xiàng),只要有一個(gè)回調(diào)函數(shù)返回true,some都會(huì)返回true,所以與every正好相反,當(dāng)遇到一個(gè)回調(diào)函數(shù)的返回值為true時(shí),可以確定結(jié)果,那么停止執(zhí)行,后面都數(shù)據(jù)不再遍歷,停在第一個(gè)返回true的位置;當(dāng)回調(diào)函數(shù)的返回值為false時(shí),需要繼續(xù)向后執(zhí)行,到最后才能確定結(jié)果,所以會(huì)遍歷所有數(shù)據(jù),實(shí)現(xiàn)類似于forEach的功能,遍歷所有
功能2:與every相反,只要有一個(gè)回調(diào)函數(shù)的返回值都為true,some的返回值為true,所有回調(diào)函數(shù)的返回值為false,some的返回值才為false
demo1:
var bool111 = arr.some(function(value,index,self){
console.log(value + "--" + index + "--" + (arr == self));
// 2--0--true
// 3--1--true
// 5--2--true
// hello--3--true
return value.length > 3;
});
console.log(arr);// 原數(shù)組
console.log(bol111); // true
demo2:
var bool112 = arr.some(function(value,index,self){
console.log(value + "--" + index + "--" + (arr == self));
// 2--0--true
return true;
});
console.log(arr);// 原數(shù)組
console.log(bool112); // true
demo3:
var bool113 = arr.some(function(value,index,self){
console.log(value + "--" + index + "--" + (arr == self));
// 2--0--true
// 3--1--true
// 5--2--true
// hello--3--true
// true--4--true
// world--5--true
// 5,6,7--6--true
// [object Object]--7--true
// function(){console.log(1)}--8--true
return false;
});
console.log(arr);// 原數(shù)組
console.log(bool113); // false
12.every(callback)
功能:判斷數(shù)組中每一項(xiàng)是否都滿足條件,只有所有項(xiàng)都滿足條件,才會(huì)返回true
參數(shù):every()接收一個(gè)回調(diào)函數(shù)作為參數(shù),這個(gè)回調(diào)函數(shù)需要有返回值,every(callback);callback默認(rèn)有三個(gè)參數(shù),分別為value,index,self
功能1:當(dāng)回調(diào)函數(shù)的返回值為true時(shí),類似于forEach的功能,遍歷所有;如果為false,那么停止執(zhí)行,后面的數(shù)據(jù)不再遍歷,停在第一個(gè)返回false的位置
功能2:當(dāng)每個(gè)回調(diào)函數(shù)的返回值都為true時(shí),every的返回值為true,只要有一個(gè)回調(diào)函數(shù)的返回值為false,every的返回值都為false
demo1:
var bool121 = arr.every(function(value,index,self){
console.log(value + "--" + index + "--" + (arr == self));
// 2--0--true
});
console.log(arr);// 原數(shù)組
console.log(bool121); // false
解釋:因?yàn)榛卣{(diào)函數(shù)中沒(méi)有return true,默認(rèn)返回undefined,等同于返回false
demo2:
var bool122 = arr.every(function(value,index,self){
console.log(value + "--" + index + "--" + (arr == self));
// 2--0--true
// 3--1--tr
// 5--2--true
// hello--3--true
// true--4--true
// world--5--true
// 5,6,7--6--true
// [object Object]--7--true
// function(){console.log(1)}--8--true
return true;
});
console.log(arr);// 原數(shù)組
console.log(bool122); // true
13.reduce(callback,initial)
功能:從數(shù)組的第一項(xiàng)開(kāi)始,逐個(gè)遍歷到最后,迭代數(shù)組的所有項(xiàng),然后構(gòu)建一個(gè)最終返回的值。
參數(shù)1:callback是回調(diào)函數(shù),表示在數(shù)組的每一項(xiàng)上調(diào)用的函數(shù)。callback默認(rèn)有四個(gè)參數(shù),分別為prev,now,index,self。callback返回的任何值都會(huì)作為下一次執(zhí)行的第一個(gè)參數(shù)prev。如果initial參數(shù)被省略,那么第一次迭代發(fā)生在數(shù)組的第二項(xiàng)上,因此callback的第一個(gè)參數(shù)是數(shù)組的第一項(xiàng),第二個(gè)參數(shù)就是數(shù)組的第二項(xiàng)。
參數(shù)2:initial,可選。作為歸并的初始值,被回調(diào)函數(shù)第一次執(zhí)行時(shí)的第一個(gè)參數(shù)prev接收
返回值:callback的return返回值
demo1(有initial,無(wú)return):
var a131 = arr.reduce(function(prev,now,index,self){
console.log(prev + "--" + now + "--" + index + "--" + (arr == self))
}, 2019);
// 2019--2--0--true
// undefined--3--1--true
// undefined--5--2--true
// undefined--hello--3--true
// undefined--true--4--true
// undefined--world--5--true
// undefined--5,6,7--6--true
// undefined--[object Object]--7--true
// undefined--function(){console.log(1)}--8--true
console.log(arr);// 原數(shù)組
console.log(a131); // undefined
解釋:回調(diào)函數(shù)沒(méi)有返回值,所以從第二次遞歸開(kāi)始,prev值為undefined
demo2(無(wú)initial,無(wú)return):
var a132 = arr.reduce(function(prev,now,index,self){
console.log(prev + "--" + now + "--" + index + "--" + (arr == self))
});
// 2--3--1--true
// undefined--5--2--true
// undefined--hello--3--true
// undefined--true--4--true
// undefined--world--5--true
// undefined--5,6,7--6--true
// undefined--[object Object]--7--true
// undefined--function(){console.log(1)}--8--true
console.log(arr);// 原數(shù)組
console.log(a132); // undefined
解釋:沒(méi)有initial,第一次的prev值是數(shù)組的第一個(gè)值,數(shù)組的第二個(gè)值作為now值;回調(diào)函數(shù)沒(méi)有返回值,所以從第二次遞歸開(kāi)始,prev值為undefined
demo3(無(wú)initial,有return):
var a133 = arr.reduce(function(prev,now,index,self){
console.log(prev + "--" + now + "--" + index + "--" + (arr == self));
return "hello";
});
// 2--3--1--true
// hello--5--2--true
// hello--hello--3--true
// hello--true--4--true
// hello--world--5--true
// hello--5,6,7--6--true
// hello--[object Object]--7--true
// hello--function(){console.log(1)}--8--true
console.log(arr);// 原數(shù)組
console.log(a133); // hello
解釋:沒(méi)有initial,第一次的prev值是數(shù)組的第一個(gè)值,數(shù)組的第二個(gè)值作為now值;回調(diào)函數(shù)有返回值,所以從第二次遞歸開(kāi)始,prev值為hello
demo4(有initial,有return):
var a134 = arr.reduce(function(prev,now,index,self){
console.log(prev + "--" + now + "--" + index + "--" + (arr == self));
return "hello";
}, 2019);
// 2019--2--0--true
// hello--3--1--true
// hello--5--2--true
// hello--hello--3--true
// hello--true--4--true
// hello--world--5--true
// hello--5,6,7--6--true
// hello--[object Object]--7--true
// hello--function(){console.log(1)}--8--true
console.log(arr);// 原數(shù)組
console.log(a134); // hello
demo5(無(wú)initial,利用reduce計(jì)算數(shù)組的和)
var arra = [2,3,4,5,6];
var sum1 = arra.reduce(function(prev,now,index,self){
return prev + now;
});
console.log(arra);// 原數(shù)組
console.log(sum1); // 20
demo6(有initial,利用reduce計(jì)算數(shù)組的和-- sum = 數(shù)組中各數(shù)組的和 + initial)
var arra = [2,3,4,5,6];
var sum2 = arra.reduce(function(prev,now,index,self){
return prev + now;
},5);
console.log(arra);// 原數(shù)組
console.log(sum2); // 25
14.reduceRight
功能:(與reduce類似)從數(shù)組的最后一項(xiàng)開(kāi)始,向前逐個(gè)遍歷到第一位,迭代數(shù)組的所有項(xiàng),然后構(gòu)建一個(gè)最終返回的值
文中部分內(nèi)容借鑒了前人的成果,也補(bǔ)充了自己的想法,如有不正確的內(nèi)容,歡迎大家指正...