日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

delete 字符数组 []_前端基础扫盲系列 长达8000字的数组总结

發布時間:2025/3/19 HTML 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 delete 字符数组 []_前端基础扫盲系列 长达8000字的数组总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文 GitHub github.com/ponkans/F2E 已收錄,有一線大廠前端面試點思維導圖,也整理了很多我的文檔,歡迎Star和完善,大家面試可以參照考點復習。文末有福利~~

前言

數組是JS最常見的一種數據結構,我們在開發中會經常用到,并且JS中數組操作函數還是非常多的,很容易出現弄混或者概念不清晰,寫下這篇文章就是想來總結一下,也算是對細節的一個知識大掃盲!!!

JS中的Array方法

新建數組

  • 常規方式


    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);//?規定了數組的長度為2
    temp2[0]?=?123;
    temp2[1]?=?"string";
    temp2[2]?=?true;?//雖然規定了數組長度,但仍可以再次添加元素,定義的數組大小對此沒有影響
  • 簡潔方式


    let?temp?=?new?Array(123,?"string",?true);
  • 數組文本方式

    let?temp?=?[123,?"string",?true];
  • 注意

    • 出于簡潔、可讀性和執行速度的考慮,推薦使用最后一種數組文本方法。

    • 數組內可以存放任意類型的數據

    • 數組元素不賦值,則為undefined

    • 訪問數組范圍以外的元素時,不會出現越界異常,為undefined

    • 定義了數組大小,依然可以添加更多的元素

增刪改查

  • 棧方法push & pop增刪數組元素

    這兩個方式都是對數組尾部進行壓入和彈出操作。

    push(arg1, arg2, …)可以每次壓入一個或多個元素,并返回更新后的數組長度。

    pop()函數則每次只會彈出尾部的元素,并返回彈出的元素,若對空數組調用pop()則返回undefined。會更改源數組。


    let?temp?=?[123,?"string",?true];

    let?push1?=?temp.push(456);?//?插入一個元素
    console.log(push1,?temp);?//?4,?[123,?"string",?true,?456]

    let?push2?=?temp.push(789,?"string2",?false);?//?插入多個元素
    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,?[];
  • 隊列方法shift & unshift增刪數組元素

    這兩個方式都是對數組頭部進行位移與彈出操作。

    unshift(arg1, arg2, …)可以像數組的頭部添加一個或多個元素,并返回更新后的數組長度,并把所有其他元素位移到更高的索引。

    shift()方法會刪除首個數組元素,并返回彈出的元素,并把所有其他元素位移到更低的索引。會更改源數組。


    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刪除數組元素

    JS數組屬于對象,所以可以使用 JavaScript delete 運算符來刪除,但這種方式會在數組留下未定義的空洞,所以很少使用。 會更改源數組。

    let?temp?=?[123,?456,?789];
    delete?temp[1];
    console.log(temp.length,?temp[1]);?//?3,?undefined
  • splice增刪數組元素

    splice(arg1, arg2, arg3, …)第一個參數定義了新添加元素的位置,第二個參數定義應刪除多少元素,其余參數定義要添加的元素,并返回一個包含已刪除項的數組。

    splice還可以在數組不留空洞的情況下刪除元素。會更改源數組。


    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合并數組

    合并現有數組來創建一個新的數組,concat(arg1, arg2, …)不會更改現有數組,總是返回一個新的數組,可以使用任意數量的數組參數。不會更改源數組。

    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]
  • 操作符合并數組,不會更改源數組。

    let?temp1?=?[1,2,3];
    let?temp2?=?[4,5,6];
    let?arr?=?[...temp1,?...temp2];
    console.log(arr);?//[1,2,3,4,5,6]
  • slice裁剪數組

    slice(arg1, arg2) 方法用數組的某個片段切出新數組,不會修改源數組中的任何元素,返回一個新數組,第一個參數為元素選取開始位置,第二個參數為元素選取結束位置,如果第二個參數被省略則會切除數組的剩余部分。不會更改源數組。

    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查數組最大值

    Math.max.apply(arg1,arg2)參數不支持數組

    可以用Math.max.apply(null,arr)來獲取數組中的最大值

  • let?temp?=?[1,3,6,2,8,4];
    let?max1?=?Math.max.apply(null,?temp);
    console.log(max1);?//?8
  • Math.min查數組最小值

    Math.min.apply(arg1,arg2)參數不支持數組

    可以用Math.min.apply(null,arr)來獲取數組中的最小值

    let?temp?=?[1,3,6,2,8,4];
    let?min1?=?Math.min.apply(null,?temp);
    console.log(min1);?//?1
  • JavaScript方法查數組最大值

    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);//?8
  • JavaScript方法查數組最小值

    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)方法在數組中搜索元素值并返回其位置。arg1為搜索元素,arg2可選從哪里開始搜索。負值將從結尾開始的給定位置開始,并搜索到結尾。

    lastIndexOf(arg1,arg2) 與 indexOf() 類似,但是從數組結尾開始搜索。arg2可選從哪里開始搜索。負值將從結尾開始的給定位置開始,并搜索到開頭。

    findIndex() 方法返回通過測試函數的第一個數組元素的索引。

    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)) 方法返回通過測試函數的第一個數組元素的值。arg1為數組元素值, arg2為數組元素索引,arg3為數組本身。

    let?temp?=?[1,35,67,8];

    let?find1?=?temp.find(function(value){
    ????return?value?>?10;
    });
    console.log(find1);//35
  • 注意

    • splice和slice容易混淆,記住splice翻譯是拼接,slice翻譯是切片。

    • splice 會返回被刪除元素組成的數組,或者為空數組

    • pop,shift會返回那個被刪除的元素

    • push,unshift 會返回新數組長度

    • pop,push,shift,unshift,splice會改變源數組

    • indexOf,lastIndexOf,concat,slice 不會改變源數組

數組轉換

  • toString數組轉字符串

    toString()方法把每個元素轉換為字符串,然后以逗號連接輸出顯示, 不會更改源數組。


    let?temp?=?[1,2,3,4];
    let?toString1?=?temp.toString();
    console.log(toString1,?temp);//1,2,3,4???[1,2,3,4]
  • join數組轉字符串

    join()方法可以把數組轉換為字符串,不過它可以指定分隔符。

    在調用 join() 方法時,可以傳遞一個參數作為分隔符來連接每個元素。

    如果省略參數,默認使用逗號作為分隔符,這時與toString()方法轉換操作效果相同。不會更改源數組。

    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*4
  • split字符串轉數組

    split(arg1, arg2)方法是 String 對象方法,與join()方法操作正好相反。該方法可以指定兩個參數,第一個參數為分隔符,指定從哪兒進行分隔的標記;第二個參數指定要返回數組的長度。

    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]
  • 對象轉數組

    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"]]
  • 數組轉對象

    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不會改變源數組

數組排序

  • sort數組排序

    sort按照字母順序對數組進行排序, 直接修改了源數組,所以可以不用再將返回值賦給其他變量。

    該函數很適合字符串排序,如果數字按照字符串來排序,則 "25" 大于 "100",因為 "2" 大于 "1",正因如此,sort()方法在對數值排序時會產生不正確的結果。

    可以通過修正比值函數對數值進行排序。比較函數的目的是定義另一種排序順序。比較函數應該返回一個負,零或正值,當 sort() 函數比較兩個值時,會將值發送到比較函數,并根據所返回的值(負、零或正值)對這些值進行排序。會更改源數組。

    let?temp?=?["bi",?"ci",?"ai",?"di"];
    let?sort1?=?temp.sort();?//?可以不用復制給sort1,?直接執行temp.sort()這里是為方便大家直觀比較
    console.log(sort1,?temp);?//?["ai","bi","ci","di"],?["ai","bi","ci","di"]


    --?sort比值函數修正:降序?-->
    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比值函數修正:升序?-->
    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數組反轉

    reverse()反轉數組中的元素,直接修改了源數組。

    let?temp?=?[1,3,2,4];
    temp.reverse();
    console.log(temp);?//[4,2,3,1];
  • 注意

sort,reverse會更改源數組

數組迭代

數組迭代即對每個數組項進行操作

  • Array.forEach()

    forEach(function(arg1,arg2,arg3){})方法為每個數組元素調用一次函數(回調函數),arg1為數組元素值, arg2為數組元素索引,arg3為數組本身, 此方法不會更改源數組,也不會創建新數組。

    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){})方法通過對每個數組元素執行函數來創建新數組,arg1為數組元素值, arg2為數組元素索引,arg3為數組本身,方法不會對沒有值的數組元素執行函數,不會更改源數組,創建一個新數組。

    let?temp?=?[1,?3,?5,?,?9];

    //index,?value不用時可以省略
    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){})方法創建一個包含通過指定條件的數組元素的新數組, arg1為數組元素值, arg2為數組元素索引,arg3為數組本身,不會更改源數組。

    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){})方法測試數組的所有元素是否通過了置頂條件。arg1為數組元素值, arg2為數組元素索引,arg3為數組本身。不會更改源數組。

    let?temp?=?[3,?5,?7,?9];

    let?every1?=?temp.every((value)?=>?{
    ????return?value?>?1;
    });
    console.log(every1);?//true
  • Array.some()

    some(function(arg1,arg2,arg3){})方法測試數組中是否有元素通過了指定條件的測試。不會更改源數組。

    let?temp?=?[3,?5,?7,?9];

    let?some1?=?temp.some(function(value){
    ????return?value?>?6;
    });
    console.log(some1);?//?true
  • Array.reduce()

    reduce(function(arg1,arg2,arg3,arg4){})接收一個函數作為累加器(accumulator),數組中的每個值(從左到右)開始縮減,最終為一個值。

    arg1上一次調用回調返回的值,或者是提供的初始值(initialValue),arg2為數組元素值, arg3為數組元素索引,arg4為數組本身。不會更改源數組。

    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循環

    性能比較好

    let?temp?=?[1,2,3,4];
    for(let?i=0,len=temp.length;i????console.log(temp[i]);
    }
    //1
    //2
    //3
    //4
  • for…in

    let?temp?=?[1,22,33,4];
    for(let?i?in?temp){
    ????console.log(i,?temp[i]);
    }
    //0?1
    //1?22
    //3?22
    //4?4
  • for…of

    for..of 是es6中引進的循環,主要是為了補全之前for循環中的以下不足

    let?temp?=?[1,2,3,4];
    for(let?i?of?temp){
    ????console.log(i);
    }
    //1
    //2
    //3
    //4
  • 跳出循環

    • forEach跳出循環

      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]?跳出本次循環
      +?return?true:?[1,3,4]?跳出本次循環
      +?return?false:?[1,3,4]?跳出本次循環
      ***/
    • map跳出循環

      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]?跳出本次循環
      +?return?true:?[1,true,3,4]?跳出本次循環
      +?return?false:?[1,false,3,4]?跳出本次循環
      ***/
    • filter跳出循環

      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]?跳出本次循環
      +?return?true:?[9,3,4]?跳出本次循環,?返回了9是因為return?true符合條件
      +?return?false:?[3,4]?跳出本次循環
      ***/
    • every跳出循環

      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會跳出循環,方便測試這里返回return?true
      });
      console.log(arr);
      /***
      +?break:?Uncaught?SyntaxError:?Illegal?break?statement?不合法
      +?continue:?Uncaught?SyntaxError:?Illegal?continue?statement?不合法
      +?return:?[1]?成功跳出循環
      +?return?true:?[1,2,3,4]?跳出本次循環
      +?return?false:?[1]?成功跳出循環
      ***/
    • some跳出循環

      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會跳出循環,方便測試這里返回return?false
      });
      console.log(arr);
      /***
      +?break:?Uncaught?SyntaxError:?Illegal?break?statement?不合法
      +?continue:?Uncaught?SyntaxError:?Illegal?continue?statement?不合法
      +?return:?[1,2,3,4]?跳出本次循環
      +?return?true:?[1]?成功跳出循環
      +?return?false:?[1,2,3,4]跳出本次循環
      ***/
    • for跳出循環

      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]?成功跳出循環
      +?continue:?[1,2,3,4]?跳出本次循環
      +?return:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
      +?return?true:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
      +?return?false:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
      ***/
    • for…in跳出循環

      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]?無效
      +?continue:?[1,9,2,3,4]?無效
      +?return:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
      +?return?true:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
      +?return?false:?Uncaught?SyntaxError:?Illegal?return?statement?不合法
      ***/
    • for…of跳出循環

      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]?成功跳出循環
      +?continue:?[1,2,3,4]?跳出本次循環
      +?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這些迭代方法不會改變源數組

    • some 在有true的時候停止

    • every 在有false的時候停止

其他常用操作

  • 檢測數組

    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]
  • 洗牌算法

    將一個數組打亂,返回一個打亂的數組

    let?temp?=?[1,3,5,6,7,2,4];
    temp.sort(()?=>?{
    ????return?Math.random()?-?0.5;
    })
    console.log(temp);
  • 數組去重

    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]
    --?數組下標判斷法:?如果在arr數組里面找當前的值,返回的索引等于當前的循環里面的i的話,那么證明這個值是第一次出現,所以推入到新數組里面,如果后面又遍歷到了一個出現過的值,那也不會返回它的索引,indexof()方法只返回找到的第一個值的索引,所以重復的都會被pass掉,只出現一次的值都被存入新數組中。?-->
    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()方法把arr排序,那么排完序后,相同的一定是挨在一起的,把它去掉就好了。首先給新數組初始化一個arr[0],因為我們要用它和arr數組進行比較,所以,for循環里面i也是從1開始了,我們讓遍歷到的arr中的值和新數組最后一位進行比較,如果相等,則pass掉,不相等的,push進來?-->

    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循環去重?-->
    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]
  • 數組去虛值

    虛值有 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]
  • 用數據填充數組

    let??arr?=?new?Array(5).fill(1);
    console.log(arr);//[1,1,1,1,1]
  • 數組中獲取隨機值

    根據數組長度獲取一個隨機索引。

    let?arr?=?['a','b','c','d'];
    let?randomIndex?=?Math.floor(Math.random()*arr.length);
    let?randomValue?=?arr[randomIndex];
    console.log(randomIndex,?randomValue);?//?2?c

硬核的文章像極了愛情,剛開始興奮就要結束了,非常感謝各位小伙伴能夠看到這里,如果覺得文章還有點東西,求點贊? 求關注?? ?求分享?!

微信搜索公眾號【接水怪】,查看更多接水怪原創~

總結

以上是生活随笔為你收集整理的delete 字符数组 []_前端基础扫盲系列 长达8000字的数组总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。