數(shù)組的一些處理方法

數(shù)組的定義

數(shù)組的定義

        // 定義一個空數(shù)組
        // 第一種方法:利用Array對象來定義數(shù)組
        let arr1 = new Array;
        console.log(arr1); //Array[0]
        // 第二種方法:直接定義一個數(shù)組
        let arr2 = [];
        console.log(arr2); //Array[0]
        // 這兩種方法實現(xiàn)的結(jié)果都是一樣的,但是更推薦第二種

數(shù)組添加元素

        // 對數(shù)組添加元素
        // 先定義一個數(shù)組,里面保存三個元素
        let color = ["red","blue","sky"];
        // 向數(shù)組的最后面添加第四個元素
        color[3] = "green";
        console.log(color) //["red", "blue", "sky", "green"]
        // 3是數(shù)組的下標(biāo),數(shù)組的下標(biāo)從0開始,0對應(yīng)第一個元素,依次向下,第四個元素的下標(biāo)為3

        // 修改數(shù)組的當(dāng)中的元素
        color[2] = "black";
        console.log(color); //["red", "blue", "black", "green"]

        //總結(jié):利用下標(biāo)修改雖然很直接,但是要知道每個元素的下標(biāo)是多少,
        //并且這樣子對數(shù)據(jù)的處理效率很低

數(shù)組的屬性

        // length:數(shù)組的長度
        let list = [1,2,3,4,5,6];
        console.log(list.length);//6
        // 數(shù)組的長度代表著里面有多少個元素,一個元素代表著一個長度,長度和下標(biāo)是不同的,每個數(shù)組都要長度,空數(shù)組長度為0

        //constructor:返回對創(chuàng)建此對象的數(shù)組函數(shù)的引用
        console.log(list.constructor); //? Array() { [native code] }

        // prototype:讓你有能力向?qū)ο筇砑訉傩院头椒?        // 向數(shù)組的原型對象添加一個數(shù)組
        Array.prototype.name = "張三";
        console.log(list.name);  //張三

數(shù)組對象的內(nèi)置屬性

  • concat
    拼接兩個數(shù)組
        let list = [1,2,3,4,5,6];
        let color = ["red","blue","sky","green"];
        // concat 拼接兩個數(shù)組
        // concat拼接兩個數(shù)組并且返回一個新的數(shù)組
        let newArr = color.concat(list);
        console.log(newArr);//["red", "blue", "black", "green", 1, 2, 3, 4, 5, 6]
  • join
        // join 把數(shù)組的所有元素放入一個字符串。元素通過指定的分隔符進(jìn)行分隔,默認(rèn)為,
        console.log(color.join()); //red,blue,black,green
        console.log(color.join("*"));//red*blue*black*green
  • pop
        // pop刪除并且返回數(shù)組最后一個元素
        let colorRemove = color.pop(); //green
  • push
        // push向數(shù)組的末尾添加一個或更多元素,并返回新的長度
        let newColor = color.push("sky","witer");//
        console.log(newColor) //5 注意是返回新的長度
  • shift
        //shift 刪除并返回數(shù)組的第一個元素
        let colorItem = color.shift();
        console.log(colorItem);//red 
  • splice
        // splice 刪除元素,并向數(shù)組添加新元素
        // 語法:arrayObject.splice(index,howmany,item1,.....,itemX)
        // index:必需。整數(shù),規(guī)定添加/刪除項目的位置,使用負(fù)數(shù)可從數(shù)組結(jié)尾處規(guī)定位置
        // howmany:必需。整數(shù),規(guī)定要刪除的個數(shù)
        // item1:可選,要添加的元素
        color.splice(1,1,'witer');
        console.log(color); //"red", "witer", "black", "green"
  • unshift
        // unshift 向數(shù)組的開頭添加一個或更多元素,并返回新的長度
        let colorLength = color.unshift("mauve");
        console.log(colorLength); //5

數(shù)組高階函數(shù)的使用

  • forEach(遍歷數(shù)組元素)
        // forEach 遍歷數(shù)組元素
        // forEach和for循環(huán)差不多,但是forEach更方便和直觀
        color.forEach( (item) => {
            console.log(item); // 循環(huán)輸出 red blue black green 
        })
  • filter(過濾,返回新數(shù)組)
        let numArr = [5,9,78,45,25,14,36];
        let newNumArr = numArr.filter( (item) => {
            return item > 9 //判斷每個數(shù)組是否大于9 大于9的話返回給新
        })
        console.log(newNumArr);//78, 45, 25, 14, 36
  • map(映射,返回新數(shù)組)
// map按照原始數(shù)組的排序方式依次對數(shù)組進(jìn)行處理
        // map返回一個新數(shù)組
        let numArr = [5,9,78,45,25,14,36];
        let newNumArr = numArr.map( (item) => {
            return item * 10 //對元素的每一個都進(jìn)行處理操作
        })
        console.log(newNumArr) //50,90,780,450,250,140,360
  • some(判斷是否含有符合條件的元素,返回布爾值)
        // some(判斷是否含有符合條件的元素,返回布爾值)
        // some() 方法用于檢測數(shù)組中的元素是否滿足指定條件(函數(shù)提供)。
        // some() 方法會依次執(zhí)行數(shù)組的每個元素:
        // 如果有一個元素滿足條件,則表達(dá)式返回true , 剩余的元素不會再執(zhí)行檢測。
        // 如果沒有滿足條件的元素,則返回false。
        let numArr = [5, 9, 78, 45, 25, 14, 36];
        let isItem = numArr.some( (item) => {
            return item > 78  //判斷是否有元素大于78的
        })
        console.log(isItem) //false
  • every(判斷是否全部元素符合條件,返回布爾值)
        // every 判斷是否全部元素都符合條件
        // every() 方法用于檢測數(shù)組所有元素是否都符合指定條件(通過函數(shù)提供)。
        // every() 方法使用指定函數(shù)檢測數(shù)組中的所有元素:
        // 如果數(shù)組中檢測到有一個元素不滿足,則整個表達(dá)式返回 false ,且剩余的元素不會再進(jìn)行檢測。
        // 如果所有元素都滿足條件,則返回 true。
        let numArr = [5, 9, 78, 45, 25, 14, 36];
        let isItem = numArr.every( (item) => {
            return item > 10
        })
        console.log(isItem);
  • reduce(累加)
        // reduce(累加)
        // reduce() 方法接收一個函數(shù)作為累加器,數(shù)組中的每個值(從左到右)開始縮減,最終計算為一個值。
        // reduce() 可以作為一個高階函數(shù),用于函數(shù)的 compose。
        // 注意: reduce() 對于空數(shù)組是不會執(zhí)行回調(diào)函數(shù)的。
        // 回調(diào)函數(shù)參數(shù):total  必需。初始值, 或者計算結(jié)束后的返回值; currentValue  必需。當(dāng)前元素; index  可選。
        // 當(dāng)前元素的索引值; arr  可選。當(dāng)前元素所屬的數(shù)組對象。
        let numArr = [5, 9, 78, 45, 25, 14, 36];
        let total = numArr.reduce( (item) => {
            return item*5 //每個元素乘以五再相加
        })
        console.log(total) //78125
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容