delete 字符数组 []_前端基础扫盲系列 长达8000字的数组总结
本文 GitHub github.com/ponkans/F2E 已收錄,有一線(xiàn)大廠(chǎng)前端面試點(diǎn)思維導(dǎo)圖,也整理了很多我的文檔,歡迎Star和完善,大家面試可以參照考點(diǎn)復(fù)習(xí)。文末有福利~~
前言
數(shù)組是JS最常見(jiàn)的一種數(shù)據(jù)結(jié)構(gòu),我們?cè)陂_(kāi)發(fā)中會(huì)經(jīng)常用到,并且JS中數(shù)組操作函數(shù)還是非常多的,很容易出現(xiàn)弄混或者概念不清晰,寫(xiě)下這篇文章就是想來(lái)總結(jié)一下,也算是對(duì)細(xì)節(jié)的一個(gè)知識(shí)大掃盲!!!
JS中的Array方法
新建數(shù)組
常規(guī)方式
let?temp?=?new?Array();
temp[0]?=?123;
temp[1]?=?"string";
temp[2]?=?true;
console.log(temp[0]);?//?123
console.log(temp[3]);?//?undefined
let?temp2?=?new?Array(2);//?規(guī)定了數(shù)組的長(zhǎng)度為2
temp2[0]?=?123;
temp2[1]?=?"string";
temp2[2]?=?true;?//雖然規(guī)定了數(shù)組長(zhǎng)度,但仍可以再次添加元素,定義的數(shù)組大小對(duì)此沒(méi)有影響簡(jiǎn)潔方式
let?temp?=?new?Array(123,?"string",?true);數(shù)組文本方式
let?temp?=?[123,?"string",?true];注意!
出于簡(jiǎn)潔、可讀性和執(zhí)行速度的考慮,推薦使用最后一種數(shù)組文本方法。
數(shù)組內(nèi)可以存放任意類(lèi)型的數(shù)據(jù)
數(shù)組元素不賦值,則為undefined
訪(fǎng)問(wèn)數(shù)組范圍以外的元素時(shí),不會(huì)出現(xiàn)越界異常,為undefined
定義了數(shù)組大小,依然可以添加更多的元素
增刪改查
棧方法push & pop增刪數(shù)組元素
這兩個(gè)方式都是對(duì)數(shù)組尾部進(jìn)行壓入和彈出操作。
push(arg1, arg2, …)可以每次壓入一個(gè)或多個(gè)元素,并返回更新后的數(shù)組長(zhǎng)度。
pop()函數(shù)則每次只會(huì)彈出尾部的元素,并返回彈出的元素,若對(duì)空數(shù)組調(diào)用pop()則返回undefined。會(huì)更改源數(shù)組。
let?temp?=?[123,?"string",?true];
let?push1?=?temp.push(456);?//?插入一個(gè)元素
console.log(push1,?temp);?//?4,?[123,?"string",?true,?456]
let?push2?=?temp.push(789,?"string2",?false);?//?插入多個(gè)元素
console.log(push2,?temp);?//?7,?[123,?"string",?true,?"456",?789,?"string2",?false]
let?temp2?=?[123];
let?pop1?=?temp2.pop();
console.log(pop1,?temp2);//?true,?[];
let?pop2?=?temp2.pop();
console.log(pop2,?temp2);?//?undefined,?[];隊(duì)列方法shift & unshift增刪數(shù)組元素
這兩個(gè)方式都是對(duì)數(shù)組頭部進(jìn)行位移與彈出操作。
unshift(arg1, arg2, …)可以像數(shù)組的頭部添加一個(gè)或多個(gè)元素,并返回更新后的數(shù)組長(zhǎng)度,并把所有其他元素位移到更高的索引。
shift()方法會(huì)刪除首個(gè)數(shù)組元素,并返回彈出的元素,并把所有其他元素位移到更低的索引。會(huì)更改源數(shù)組。
let?temp?=?[123,?456,?789];
let?unshift1?=?temp.unshift("abc",?"efg");
console.log(unshift1,?temp);?//?5,?["abc",?"efg",?123,?456,?789]
let?temp2?=?[123,?456,?789];
let?shift1?=?temp2.shift();
console.log(shift1,?temp2);?//?123,?[456,?789]delete刪除數(shù)組元素
JS數(shù)組屬于對(duì)象,所以可以使用 JavaScript delete 運(yùn)算符來(lái)刪除,但這種方式會(huì)在數(shù)組留下未定義的空洞,所以很少使用。 會(huì)更改源數(shù)組。
let?temp?=?[123,?456,?789];
delete?temp[1];
console.log(temp.length,?temp[1]);?//?3,?undefinedsplice增刪數(shù)組元素
splice(arg1, arg2, arg3, …)第一個(gè)參數(shù)定義了新添加元素的位置,第二個(gè)參數(shù)定義應(yīng)刪除多少元素,其余參數(shù)定義要添加的元素,并返回一個(gè)包含已刪除項(xiàng)的數(shù)組。
splice還可以在數(shù)組不留空洞的情況下刪除元素。會(huì)更改源數(shù)組。
let?temp1?=?[1,?2,?3,?4,?5,?6];
let?splice1?=?temp1.splice(1,?2,?7,?8,?9);
console.log(splice1,?temp1);?//[2,?3],[1,?7,?8,?9,?4,?5,?6]
let?temp2?=?[1,?2,?3,?4,?5,?6];
let?splice2?=?temp2.splice(1,?0,?7,?8);
console.log(splice2,?temp2);//[],?[1,?7,?8,?2,?3,?4,?5,?6]
let?temp3?=?[1,?2,?3,?4,?5,?6];
let?splice3?=?temp3.splice(1,?2);
console.log(splice3,?temp3);//[2,?3],?[1,?4,?5,?6]concat合并數(shù)組
合并現(xiàn)有數(shù)組來(lái)創(chuàng)建一個(gè)新的數(shù)組,concat(arg1, arg2, …)不會(huì)更改現(xiàn)有數(shù)組,總是返回一個(gè)新的數(shù)組,可以使用任意數(shù)量的數(shù)組參數(shù)。不會(huì)更改源數(shù)組。
let?temp1?=?[1,?2];
let?temp2?=?[3,?4];
let?temp3?=?[5];
let?concat1?=?temp1.concat(temp2,?temp3);
console.log(concat1,?temp1,?temp2);?//?[1,2,3,4,5],?[1,2],[3,4]操作符合并數(shù)組,不會(huì)更改源數(shù)組。
let?temp1?=?[1,2,3];
let?temp2?=?[4,5,6];
let?arr?=?[...temp1,?...temp2];
console.log(arr);?//[1,2,3,4,5,6]slice裁剪數(shù)組
slice(arg1, arg2) 方法用數(shù)組的某個(gè)片段切出新數(shù)組,不會(huì)修改源數(shù)組中的任何元素,返回一個(gè)新數(shù)組,第一個(gè)參數(shù)為元素選取開(kāi)始位置,第二個(gè)參數(shù)為元素選取結(jié)束位置,如果第二個(gè)參數(shù)被省略則會(huì)切除數(shù)組的剩余部分。不會(huì)更改源數(shù)組。
let?temp?=?[1,2,3,4,5,6,7,8];
let?slice1?=?temp.slice(2,?4);
console.log(slice1,?temp);?//[3,?4],[1,2,3,4,5,6,7,8];
let?slice2?=?temp.slice(4);
console.log(slice2);?//[5,6,7,8]Math.max查數(shù)組最大值
Math.max.apply(arg1,arg2)參數(shù)不支持?jǐn)?shù)組
可以用Math.max.apply(null,arr)來(lái)獲取數(shù)組中的最大值
- let?temp?=?[1,3,6,2,8,4];
let?max1?=?Math.max.apply(null,?temp);
console.log(max1);?//?8 Math.min查數(shù)組最小值
Math.min.apply(arg1,arg2)參數(shù)不支持?jǐn)?shù)組
可以用Math.min.apply(null,arr)來(lái)獲取數(shù)組中的最小值
let?temp?=?[1,3,6,2,8,4];
let?min1?=?Math.min.apply(null,?temp);
console.log(min1);?//?1JavaScript方法查數(shù)組最大值
function?getArrayMaxValue(arr)?{
????var?len?=?arr.length
????var?max?=?-Infinity;
????while?(len--)?{
????????if?(arr[len]?>?max)?{
????????????max?=?arr[len];
????????}
????}
????return?max;
}
let?temp?=?[1,3,6,2,8,4];
let?max1?=?getArrayMaxValue(temp);
console.log(max1);//?8JavaScript方法查數(shù)組最小值
function?getArrayMinValue(arr)?{
????var?len?=?arr.length
????var?min?=?Infinity;
????while?(len--)?{
????????if?(arr[len]?min)?{
????????????min?=?arr[len];
????????}
????}
????return?min;
}
let?temp?=?[1,3,6,2,8,4];
let?min1?=?getArrayMinValue(temp);
console.log(min1);//?1查找索引
indexOf(arg1, arg2)方法在數(shù)組中搜索元素值并返回其位置。arg1為搜索元素,arg2可選從哪里開(kāi)始搜索。負(fù)值將從結(jié)尾開(kāi)始的給定位置開(kāi)始,并搜索到結(jié)尾。
lastIndexOf(arg1,arg2) 與 indexOf() 類(lèi)似,但是從數(shù)組結(jié)尾開(kāi)始搜索。arg2可選從哪里開(kāi)始搜索。負(fù)值將從結(jié)尾開(kāi)始的給定位置開(kāi)始,并搜索到開(kāi)頭。
findIndex() 方法返回通過(guò)測(cè)試函數(shù)的第一個(gè)數(shù)組元素的索引。
let?temp?=?[1,2,3,4,5,4,3,2];
let?pos1?=?temp.indexOf(4);
console.log(pos1);?//3
let?pos2?=?temp.lastIndexOf(4);
console.log(pos2);//5
let?temp?=?[1,35,67,8];
let?findIndex1?=?temp.findIndex(function(value){
????return?value?>?10;
})
console.log(findIndex1);?//1查找值
find(function(arg1,arg2,arg3)) 方法返回通過(guò)測(cè)試函數(shù)的第一個(gè)數(shù)組元素的值。arg1為數(shù)組元素值, arg2為數(shù)組元素索引,arg3為數(shù)組本身。
let?temp?=?[1,35,67,8];
let?find1?=?temp.find(function(value){
????return?value?>?10;
});
console.log(find1);//35注意!
splice和slice容易混淆,記住splice翻譯是拼接,slice翻譯是切片。
splice 會(huì)返回被刪除元素組成的數(shù)組,或者為空數(shù)組
pop,shift會(huì)返回那個(gè)被刪除的元素
push,unshift 會(huì)返回新數(shù)組長(zhǎng)度
pop,push,shift,unshift,splice會(huì)改變?cè)磾?shù)組
indexOf,lastIndexOf,concat,slice 不會(huì)改變?cè)磾?shù)組
數(shù)組轉(zhuǎn)換
toString數(shù)組轉(zhuǎn)字符串
toString()方法把每個(gè)元素轉(zhuǎn)換為字符串,然后以逗號(hào)連接輸出顯示, 不會(huì)更改源數(shù)組。
let?temp?=?[1,2,3,4];
let?toString1?=?temp.toString();
console.log(toString1,?temp);//1,2,3,4???[1,2,3,4]join數(shù)組轉(zhuǎn)字符串
join()方法可以把數(shù)組轉(zhuǎn)換為字符串,不過(guò)它可以指定分隔符。
在調(diào)用 join() 方法時(shí),可以傳遞一個(gè)參數(shù)作為分隔符來(lái)連接每個(gè)元素。
如果省略參數(shù),默認(rèn)使用逗號(hào)作為分隔符,這時(shí)與toString()方法轉(zhuǎn)換操作效果相同。不會(huì)更改源數(shù)組。
let?temp?=?[1,2,3,4];
let?join1?=?temp.join();
let?join2?=?temp.join("*");
console.log(join1,?join2);?//1,2,3,4???1*2*3*4split字符串轉(zhuǎn)數(shù)組
split(arg1, arg2)方法是 String 對(duì)象方法,與join()方法操作正好相反。該方法可以指定兩個(gè)參數(shù),第一個(gè)參數(shù)為分隔符,指定從哪兒進(jìn)行分隔的標(biāo)記;第二個(gè)參數(shù)指定要返回?cái)?shù)組的長(zhǎng)度。
let?temp?=?"1-2-3-4-5";
let?split1?=?temp.split("-");
let?split2?=temp.split("-",?2);
console.log(split1,?split2);//[1,2,3,4,5],[1,2]對(duì)象轉(zhuǎn)數(shù)組
let?temp?=?{key1:?"value1",?key2:?"value2"};
let?trans1?=?Object.keys(temp);
console.log(trans1,?temp);//["key1",?"key2"],??{key1:?"value1",?key2:?"value2"}
let?trans2?=?Object.values(temp);
console.log(trans2);?//["value1",?"value2"]
let?trans3?=?Object.entries(temp);
console.log(trans3);?//[["key1",?"key2"],?["value1",?"value2"]]數(shù)組轉(zhuǎn)對(duì)象
let?temp?=?["a","b"];
let?trans1?=?{...temp};
console.log(trans1,?temp);?//?{0:?"a",?1:?"b"},?["a","b"]
let?trans2?=?Object.assign({},?temp);;
console.log(trans2,?temp);//{0:?"a",?1:?"b"},?["a","b"]
let?trans3?=?Object.assign(temp,?{});
console.log(trans3,?temp);?//?["a","b"],?["a","b"]
注意!
toString, join不會(huì)改變?cè)磾?shù)組
數(shù)組排序
sort數(shù)組排序
sort按照字母順序?qū)?shù)組進(jìn)行排序, 直接修改了源數(shù)組,所以可以不用再將返回值賦給其他變量。
該函數(shù)很適合字符串排序,如果數(shù)字按照字符串來(lái)排序,則 "25" 大于 "100",因?yàn)?"2" 大于 "1",正因如此,sort()方法在對(duì)數(shù)值排序時(shí)會(huì)產(chǎn)生不正確的結(jié)果。
可以通過(guò)修正比值函數(shù)對(duì)數(shù)值進(jìn)行排序。比較函數(shù)的目的是定義另一種排序順序。比較函數(shù)應(yīng)該返回一個(gè)負(fù),零或正值,當(dāng) sort() 函數(shù)比較兩個(gè)值時(shí),會(huì)將值發(fā)送到比較函數(shù),并根據(jù)所返回的值(負(fù)、零或正值)對(duì)這些值進(jìn)行排序。會(huì)更改源數(shù)組。
let?temp?=?["bi",?"ci",?"ai",?"di"];
let?sort1?=?temp.sort();?//?可以不用復(fù)制給sort1,?直接執(zhí)行temp.sort()這里是為方便大家直觀(guān)比較
console.log(sort1,?temp);?//?["ai","bi","ci","di"],?["ai","bi","ci","di"]
--?sort比值函數(shù)修正:降序?-->
let?temp?=?[40,?100,?1,?5,?25,?10];
temp.sort(function(a,?b){
????return?b-a
});
console.log(temp);?//?[100,?40,?25,?10,?5,?1]
--?sort比值函數(shù)修正:升序?-->
let?temp?=?[40,?100,?1,?5,?25,?10];
temp.sort(function(a,?b){
????return?a-b;
});
console.log(temp);?//?[1,?5,?10,?25,?40,?100]reverse數(shù)組反轉(zhuǎn)
reverse()反轉(zhuǎn)數(shù)組中的元素,直接修改了源數(shù)組。
let?temp?=?[1,3,2,4];
temp.reverse();
console.log(temp);?//[4,2,3,1];注意!
sort,reverse會(huì)更改源數(shù)組
數(shù)組迭代
數(shù)組迭代即對(duì)每個(gè)數(shù)組項(xiàng)進(jìn)行操作
Array.forEach()
forEach(function(arg1,arg2,arg3){})方法為每個(gè)數(shù)組元素調(diào)用一次函數(shù)(回調(diào)函數(shù)),arg1為數(shù)組元素值, arg2為數(shù)組元素索引,arg3為數(shù)組本身, 此方法不會(huì)更改源數(shù)組,也不會(huì)創(chuàng)建新數(shù)組。
let?temp?=?[1,3,5];
temp.forEach(function(value,?index,?array){
????console.log(value,?index,?array);
});
/***
*1?0?[1,3,5]
*3?1?[1,3,5]
*5?2?[1,3,5]
***/Array.map()
map(function(arg1,arg2,arg3){})方法通過(guò)對(duì)每個(gè)數(shù)組元素執(zhí)行函數(shù)來(lái)創(chuàng)建新數(shù)組,arg1為數(shù)組元素值, arg2為數(shù)組元素索引,arg3為數(shù)組本身,方法不會(huì)對(duì)沒(méi)有值的數(shù)組元素執(zhí)行函數(shù),不會(huì)更改源數(shù)組,創(chuàng)建一個(gè)新數(shù)組。
let?temp?=?[1,?3,?5,?,?9];
//index,?value不用時(shí)可以省略
let?map1?=?temp.map(function(value,?index,?array){?
????return?value*2
});
console.log(map1);//?[2,?6,?10,?empty,?18]Array.filter()
filter(function(arg1,arg2,arg3){})方法創(chuàng)建一個(gè)包含通過(guò)指定條件的數(shù)組元素的新數(shù)組, arg1為數(shù)組元素值, arg2為數(shù)組元素索引,arg3為數(shù)組本身,不會(huì)更改源數(shù)組。
let?temp?=?[1,?3,?5,?7,?9];
let?filter1?=?temp.filter(function(value){
????return?value?>?5;
});
console.log(filter1);?//?[7,?9]Array.every()
every(function(arg1,arg2,arg3){})方法測(cè)試數(shù)組的所有元素是否通過(guò)了置頂條件。arg1為數(shù)組元素值, arg2為數(shù)組元素索引,arg3為數(shù)組本身。不會(huì)更改源數(shù)組。
let?temp?=?[3,?5,?7,?9];
let?every1?=?temp.every((value)?=>?{
????return?value?>?1;
});
console.log(every1);?//trueArray.some()
some(function(arg1,arg2,arg3){})方法測(cè)試數(shù)組中是否有元素通過(guò)了指定條件的測(cè)試。不會(huì)更改源數(shù)組。
let?temp?=?[3,?5,?7,?9];
let?some1?=?temp.some(function(value){
????return?value?>?6;
});
console.log(some1);?//?trueArray.reduce()
reduce(function(arg1,arg2,arg3,arg4){})接收一個(gè)函數(shù)作為累加器(accumulator),數(shù)組中的每個(gè)值(從左到右)開(kāi)始縮減,最終為一個(gè)值。
arg1上一次調(diào)用回調(diào)返回的值,或者是提供的初始值(initialValue),arg2為數(shù)組元素值, arg3為數(shù)組元素索引,arg4為數(shù)組本身。不會(huì)更改源數(shù)組。
let?temp?=?[3,?5,?7,?9];
let?reduce1?=?temp.reduce(function(a,b){
????console.log(a,b);
????return?a+b;
});
console.log(reduce1);
/***
*3?5
*8?7
*15?9
*24
***/普通for循環(huán)
性能比較好
let?temp?=?[1,2,3,4];
for(let?i=0,len=temp.length;i????console.log(temp[i]);
}
//1
//2
//3
//4for…in
let?temp?=?[1,22,33,4];
for(let?i?in?temp){
????console.log(i,?temp[i]);
}
//0?1
//1?22
//3?22
//4?4for…of
for..of 是es6中引進(jìn)的循環(huán),主要是為了補(bǔ)全之前for循環(huán)中的以下不足
let?temp?=?[1,2,3,4];
for(let?i?of?temp){
????console.log(i);
}
//1
//2
//3
//4跳出循環(huán)
forEach跳出循環(huán)
let?temp?=?[1,2,3,4];
let?arr?=?[];
temp.forEach((value,?index)?=>{
????if(index?===?1){
????????//?break;
????????//?continue;
????????//?return;
????????//?return?true;
????????return?false;
????}
????arr.push(value);
});
console.log(arr);
/***
+?break:?Uncaught?SyntaxError:?Illegal?break?statement?不合法
+?continue:?Uncaught?SyntaxError:?Illegal?continue?statement?不合法
+?return:?[1,3,4]?跳出本次循環(huán)
+?return?true:?[1,3,4]?跳出本次循環(huán)
+?return?false:?[1,3,4]?跳出本次循環(huán)
***/map跳出循環(huán)
let?temp?=?[1,2,3,4];
let?arr?=?temp.map((value,?index)?=>{
????if(index?===?1){
????????//?break;
????????//?continue;
????????//?return;
????????//?return?true;
????????//?return?false;
????}
????return?value;
});
console.log(arr);
/***
+?break:?Uncaught?SyntaxError:?Illegal?break?statement?不合法
+?continue:?Uncaught?SyntaxError:?Illegal?continue?statement?不合法
+?return:?[1,undefined,3,4]?跳出本次循環(huán)
+?return?true:?[1,true,3,4]?跳出本次循環(huán)
+?return?false:?[1,false,3,4]?跳出本次循環(huán)
***/filter跳出循環(huán)
let?temp?=?[1,9,2,3,4];
let?arr?=?temp.filter((value,?index)?=>{
????if(index?===?1){
????????//?break;
????????//?continue;
????????//?return;
????????//?return?true;
????????//?return?false;
????}
????return?value?>?2;
});
console.log(arr);
/***
+?break:?Uncaught?SyntaxError:?Illegal?break?statement?不合法
+?continue:?Uncaught?SyntaxError:?Illegal?continue?statement?不合法
+?return:?[3,4]?跳出本次循環(huán)
+?return?true:?[9,3,4]?跳出本次循環(huán),?返回了9是因?yàn)閞eturn?true符合條件
+?return?false:?[3,4]?跳出本次循環(huán)
***/every跳出循環(huán)
let?temp?=?[1,9,2,3,4];
let?arr?=?[];
temp.every((value,?index)?=>{
????if(index?===?1){
????????//?break;
????????//?continue;
????????//?return;
????????//?return?true;
????????//?return?false;
????}
????arr.push(value);
????return?true;?//?every遇到return?false會(huì)跳出循環(huán),方便測(cè)試這里返回return?true
});
console.log(arr);
/***
+?break:?Uncaught?SyntaxError:?Illegal?break?statement?不合法
+?continue:?Uncaught?SyntaxError:?Illegal?continue?statement?不合法
+?return:?[1]?成功跳出循環(huán)
+?return?true:?[1,2,3,4]?跳出本次循環(huán)
+?return?false:?[1]?成功跳出循環(huán)
***/some跳出循環(huán)
let?temp?=?[1,9,2,3,4];
let?arr?=?[];
temp.some((value,?index)?=>{
????if(index?===?1){
????????//?break;
????????//?continue;
????????//?return;
????????//?return?true;
????????//?return?false;
????}
????arr.push(value);
????return?false;?//?every遇到return?true會(huì)跳出循環(huán),方便測(cè)試這里返回return?false
});
console.log(arr);
/***
+?break:?Uncaught?SyntaxError:?Illegal?break?statement?不合法
+?continue:?Uncaught?SyntaxError:?Illegal?continue?statement?不合法
+?return:?[1,2,3,4]?跳出本次循環(huán)
+?return?true:?[1]?成功跳出循環(huán)
+?return?false:?[1,2,3,4]跳出本次循環(huán)
***/for跳出循環(huán)
let?temp?=?[1,9,2,3,4];
let?arr?=?[];
for(let?i?=?0;?i??????if(i?===?1){
????????//?break;
????????//?continue;
????????//?return;
????????//?return?true;
????????//?return?false;
????}
????arr.push(temp[i]);
}
console.log(arr);
/***
+?break:?[1]?成功跳出循環(huán)
+?continue:?[1,2,3,4]?跳出本次循環(huán)
+?return:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
+?return?true:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
+?return?false:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
***/for…in跳出循環(huán)
let?temp?=?[1,9,2,3,4];
let?arr?=?[];
for(let?index?in?temp){
????if(index?===?1){
????????//?break;
????????//?continue;
????????//?return;
????????//?return?true;
????????//?return?false;
????}
????arr.push(temp[index]);
}
console.log(arr);
/***
+?break:?[1,9,2,3,4]?無(wú)效
+?continue:?[1,9,2,3,4]?無(wú)效
+?return:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
+?return?true:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
+?return?false:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
***/for…of跳出循環(huán)
let?temp?=?[1,9,2,3,4];
let?arr?=?[];
let?index?=1
for(let?value?of?temp){
????if(value?===?9){
????????//?break;
????????//?continue;
????????//?return;
????????//?return?true;
????????//?return?false;
????}
????arr.push(value);
}
console.log(arr);
/***
+?break:?[1]?成功跳出循環(huán)
+?continue:?[1,2,3,4]?跳出本次循環(huán)
+?return:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
+?return?true:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
+?return?false:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
***/匯總表格:
注意!
forEach,map,filter,every,some,reduce這些迭代方法不會(huì)改變?cè)磾?shù)組
some 在有true的時(shí)候停止
every 在有false的時(shí)候停止
其他常用操作
檢測(cè)數(shù)組
let?temp1?=?[1,2,4];
let?temp2?=?5;
console.log(temp1?instanceof?Array);?//?true
console.log(temp2?instanceof?Array);?//false
console.log(Array.isArray(temp1));?//true
console.log(Array.isArray(temp2));?//false
console.log(Object.prototype.toString.call(temp1));?//[object?Array]
console.log(Object.prototype.toString.call(temp2));//?[object?Number]洗牌算法
將一個(gè)數(shù)組打亂,返回一個(gè)打亂的數(shù)組
let?temp?=?[1,3,5,6,7,2,4];
temp.sort(()?=>?{
????return?Math.random()?-?0.5;
})
console.log(temp);數(shù)組去重
let?temp?=?[1,3,5,6,7,9,4,3,1,6];
let?unique1?=?Array.from(new?Set(temp));
console.log(unique1);//[1,?3,?5,?6,?7,?9,?4]
let?unique2?=?[...new?Set(temp)];
console.log(unique2);//[1,?3,?5,?6,?7,?9,?4]
let?newArr?=?[];
for(let?i=0;?i<temp.length;i++){if(newArr.indexOf(temp[i])?===?-1){newArr.push(temp[i]);
????}
}console.log(newArr);//[1,?3,?5,?6,?7,?9,?4]
--?數(shù)組下標(biāo)判斷法:?如果在arr數(shù)組里面找當(dāng)前的值,返回的索引等于當(dāng)前的循環(huán)里面的i的話(huà),那么證明這個(gè)值是第一次出現(xiàn),所以推入到新數(shù)組里面,如果后面又遍歷到了一個(gè)出現(xiàn)過(guò)的值,那也不會(huì)返回它的索引,indexof()方法只返回找到的第一個(gè)值的索引,所以重復(fù)的都會(huì)被pass掉,只出現(xiàn)一次的值都被存入新數(shù)組中。?-->
let?newArr?=?[];
for(let?i=0;?i<temp.length;i++){if(temp.indexOf(temp[i])?===?i){newArr.push(temp[i]);
????}
}console.log(newArr);//[1,?3,?5,?6,?7,?9,?4]
--?排序后相鄰去除法:?先用sort()方法把a(bǔ)rr排序,那么排完序后,相同的一定是挨在一起的,把它去掉就好了。首先給新數(shù)組初始化一個(gè)arr[0],因?yàn)槲覀円盟蚢rr數(shù)組進(jìn)行比較,所以,for循環(huán)里面i也是從1開(kāi)始了,我們讓遍歷到的arr中的值和新數(shù)組最后一位進(jìn)行比較,如果相等,則pass掉,不相等的,push進(jìn)來(lái)?-->
let?arr?=?[1,3,5,6,7,9,4,3,1,6];
arr.sort();
let?newArr?=?[arr[0]];
for(let?i=1;?i<arr.length;i++){if(arr[i]?!==?newArr[newArr.length?-?1]){newArr.push(arr[i]);
????}
}console.log(newArr);?//[1,?3,?4,?5,?6,?7,?9]
--?雙層for循環(huán)去重?-->
let?arr?=?[1,3,5,6,7,9,4,3,1,6];
for(let?i=0;?i<arr.length;i++){for(let?j=i+1;?j<arr.length;?j++){if(arr[i]?===?arr[j]){arr.splice(j,1);j--;
????????}
????}
}console.log(arr);//?[1,?3,?5,?6,?7,?9,?4]數(shù)組去虛值
虛值有 false, 0,'', null, NaN, undefined
let?temp?=?[1,2,"",4,undefined,5,?false,0,null,NaN];
let?newArr?=?temp.filter((value)?=>?{
????return?value;
})
console.log(newArr);?//[1,2,4,5]用數(shù)據(jù)填充數(shù)組
let??arr?=?new?Array(5).fill(1);
console.log(arr);//[1,1,1,1,1]數(shù)組中獲取隨機(jī)值
根據(jù)數(shù)組長(zhǎng)度獲取一個(gè)隨機(jī)索引。
let?arr?=?['a','b','c','d'];
let?randomIndex?=?Math.floor(Math.random()*arr.length);
let?randomValue?=?arr[randomIndex];
console.log(randomIndex,?randomValue);?//?2?c
硬核的文章像極了愛(ài)情,剛開(kāi)始興奮就要結(jié)束了,非常感謝各位小伙伴能夠看到這里,如果覺(jué)得文章還有點(diǎn)東西,求點(diǎn)贊? 求關(guān)注?? ?求分享?!
微信搜索公眾號(hào)【接水怪】,查看更多接水怪原創(chuàng)~
總結(jié)
以上是生活随笔為你收集整理的delete 字符数组 []_前端基础扫盲系列 长达8000字的数组总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql单库tps_MySQL数据库三
- 下一篇: 2017年html5行业报告,云适配发布