JS 數組 常用方法

来源:https://www.cnblogs.com/l-y-h/archive/2020/01/04/12150578.html
-Advertisement-
Play Games

一、數組 1、function(value, index, array) {} 【格式:】 function (value, index, array) => { // value 指 數組當前遍歷的值, index 指 數組當前遍歷的下標, array 指 當前數組 // ... 自定義函數行為 ...


一、數組

1、function(value, index, array) {}

【格式:】

function (value, index, array) => {
    // value 指 數組當前遍歷的值, index 指 數組當前遍歷的下標, array 指 當前數組
    // ... 自定義函數行為
    // return ...;
}

 

2、Array.map(function() {})

  返回值:一個新數組。
  簡單理解為:此方法用於 根據 自定義執行函數 處理數組中的每個元素,並作為一個新數組 返回,不會改變原來的數組(除非主動修改原數組的值)。

【舉例:給數組中的每個元素值加 6,並生成一個新數組】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.map((value, index, array) => {
    // value 指 數組當前遍歷的值, index 指 數組當前遍歷的下標, array 指 當前數組
    return array[index] += 6;
});
console.log(newArr);   // 輸出 [7, 8, 9, 10, 11]
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [7, 8, 9, 10, 11]
console.log(newArr);  // 輸出 [7, 8, 9, 10, 11]

 

 

 

3、Array.forEach(function() {})

  返回值:undefined,即沒有返回值。
  簡單理解為:此方法用於 根據 自定義執行函數 處理數組中的每個元素,返回 undefined(沒有返回值),不會改變原來的數組(除非主動修改原數組的值)。
  註:
    與 map 方法的區別在於 沒有返回值。

【舉例:給數組中的每個元素值加 6,不會生成一個新數組】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.forEach((value, index, array) => {
    // value 指 數組當前遍歷的值, index 指 數組當前遍歷的下標, array 指 當前數組
    return array[index] += 6;
});
console.log(newArr);   // 輸出 undefined
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [7, 8, 9, 10, 11]
console.log(newArr);  // 輸出 undefined

 

 

 

4、Array.filter(function() {})

  返回值:返回一個新數組。
  簡單理解為:此方法用於 根據 自定義執行函數 處理數組中的每個元素,過濾原數組,並返回一個新數組,不會改變原來的數組(除非主動修改原數組的值)。
  註:
    與 map 方法的區別在於 返回的是一個過濾後的數組。

【舉例:過濾數組中 大於 3 的數據,並作為一個新數組】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.filter((value, index, array) => {
    // value 指 數組當前遍歷的值, index 指 數組當前遍歷的下標, array 指 當前數組
    return value > 3;
});
console.log(newArr);   // 輸出 [4, 5]
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
console.log(newArr);  // 輸出 [4, 5]

 

 

 

5、Array.every(function() {})

  返回值:一個 布爾值。
  簡單理解為:此方法用於 根據 自定義執行函數 處理數組中的每個元素,並返回一個布爾值,不會改變原來的數組(除非主動修改原數組的值)。若返回值為 true,則表示 數組每個元素均滿足 function。否則,返回值為 false。
  註:
    與 map 方法的區別在於 其返回的是一個 布爾值。

【舉例:比較數組的每個值,若值均大於 3,則返回 true,否則返回 false】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.every((value, index, array) => {
    // value 指 數組當前遍歷的值, index 指 數組當前遍歷的下標, array 指 當前數組
    return value > 3;
});
console.log(newArr);   // 輸出 false
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
console.log(newArr);  // 輸出 false

 

 

 

6、Array.some(function() {})

  返回值:一個 布爾值。
  簡單理解為:此方法用於 根據 自定義執行函數 處理數組中的每個元素,並返回一個布爾值,不會改變原來的數組(除非主動修改原數組的值)。若返回值為 true,則表示 數組每個元素有部分滿足 function。返回值為 false,則表示數組每個元素 均不滿足 function。
  註:
    與 map 方法的區別在於 其返回的是一個 布爾值。

【舉例:比較數組的每個值,若部分值大於 3,則返回 true,若值全小於 3,則返回 false】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.some((value, index, array) => {
    // value 指 數組當前遍歷的值, index 指 數組當前遍歷的下標, array 指 當前數組
    return value > 3;
});
console.log(newArr);   // 輸出 true
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
console.log(newArr);  // 輸出 true

 

 

 

7、Array.push(data)

  返回值:當前數組的長度。會改變原數組。
  簡單理解為:向數組的末尾添加一個數據,並返回添加後的數組長度。

【舉例:向數組末尾添加一個數據,並返回添加後的數組長度】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.push(1);
console.log(newArr);   // 輸出 6
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [1, 2, 3, 4, 5, 1]
console.log(newArr);  // 輸出 6

 

 

 

8、Array.pop()

  返回值:當前刪除的數據。會改變原數組。
  簡單理解為:從數組的末尾刪除最後一個數據,並返回刪除後的數組數據。

【舉例:從數組末尾刪除最後一個數據,並返回當前刪除的數據】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.pop();
console.log(newArr);   // 輸出 5
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [1, 2, 3, 4]
console.log(newArr);  // 輸出 5

 

 

 

9、Array.shift()

  返回值:當前刪除的數據。會改變原數組。
  簡單理解為:從數組的頭部刪除第一個數據,並返回刪除後的數組數據。

【舉例:刪除數組頭部的第一個元素,並返回該值】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.shift();
console.log(newArr);   // 輸出 1
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [2, 3, 4, 5]
console.log(newArr);  // 輸出 1

 

 

 

10、Array.unshift(data1, data2)

  返回值:當前數組的長度,會改變原數組。
  簡單理解為:將一些數據添加到數組的頭部。

【舉例:將 1, 2, 4 添加到數組的頭部】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.unshift(1, 2, 4);
console.log(newArr);   // 輸出 8
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [1, 2, 4, 1, 2, 3, 4, 5]
console.log(newArr);  // 輸出 8

 

 

 

11、Array.concat(Array)

  返回值:一個新的數組。不會改變原數組(除非主動修改)。
  簡單理解為:兩個數組拼接成一個數組。

【舉例:兩數組拼接,返回一個新數組】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.concat([1, 2, 3, 4, 5]);
console.log(newArr);   // 輸出 [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
console.log(newArr);  // 輸出 [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

 

 

 

12、Array.toString()

  返回值:一個字元串。不會改變原數組(除非主動修改)。
  簡單理解為:將當前數組轉為字元串輸出,不改變原數組。

【舉例:將數組轉為字元串,不改變原數組】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.toString();
console.log(newArr);   // 輸出 1, 2, 3, 4, 5
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
console.log(newArr);  // 輸出 1, 2, 3, 4, 5

 

 

 

13、Array.join()

  返回值:一個字元串。不會改變原數組(除非主動修改)。
  簡單理解為:將當前數組轉為字元串輸出,不改變原數組。
  註:
    與 toString 方法的區別在於,可以自定義數據間連接的符號(預設以逗號分隔)。

【舉例:將數組轉為字元串,並以 * 分隔】

let arr = [1, 2, 3, 4, 5];
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
let newArr = arr.join('*');
console.log(newArr);   // 輸出 1*2*3*4*5
console.log(arr === newArr);  // 輸出 false
console.log(arr);   // 輸出 [1, 2, 3, 4, 5]
console.log(newArr);  // 輸出 1*2*3*4*5

 

 

 

14、Array.splice(開始位置(必須), 刪除個數(必須), 新元素...(可選))

  返回值:一個新數組(刪除的元素組成的數組)。會改變原數組。
  簡單理解為:對數組進行增、刪、改。
    若當前方法參數只有兩個必須項,沒有新元素,則進行的是刪除操作。

【舉例:對數組元素進行增刪改】

【舉例:對數組元素進行增刪改】

let arr = [1, 2, 3, 4, 5];
console.log(arr);       // 輸出 [1, 2, 3, 4, 5]
// 在數組下標為 2 的地方,新增 2 個元素,未進行刪除操作,返回 []
let arr1 = arr.splice(2, 0, 7, 8);
console.log(arr);       // 輸出 [1, 2, 7, 8, 3, 4, 5]
console.log(arr1);      // []

// 在數組下標為 2 的地方,刪除 3 個元素,進行刪除操作,返回刪除的數組
let arr2 = arr.splice(2, 3)
console.log(arr);        // 輸出 [1, 2, 4, 5]
console.log(arr2);        // 輸出 [7, 8, 3]

// 在數組下標為 2 的地方,刪除 1 個元素,並新增 2 個元素,返回刪除的數組
let arr3 = arr.splice(2, 1, 8, 6);
console.log(arr);        // 輸出 [1, 2, 8, 6, 5]
console.log(arr3);        // 輸出 [4]


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 場景 Android佈局管理器-使用LinearLayout實現簡單的登錄視窗佈局: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103838995 幀佈局管理器FrameLayout 實現效果 註: 博客: https://b ...
  • 場景 Android佈局管理器-從實例入手學習相對佈局管理器的使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103838924 線性佈局LinearLayout,分為水平和垂直線性佈局。 實現效果如下 註: 博客: htt ...
  • 場景 AndroidStudio跑起來第一個App時新手遇到的那些坑: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103797243 使用相對佈局RelativeLayout實現簡單的登錄提示的佈局,效果如下 註: 博客: h ...
  • 最近有一些開發朋友問我應該怎樣提升自己的能力,回想起來做了這麼久 iOS 開發,我也有過那種“讓我做一個功能實現個需求我會做,但接下來怎樣提高我不知道。”的時期,這裡嘗試列一下 iOS 開發的相關技術,再說說在學習進階上我的一些想法。 iOS 技術棧 這裡按我的理解給 iOS 相關技術分個類,以工程 ...
  • 1.壓力測試monkey 通過cmd輸入下麵命令: 表示測試com.example.phonecall應用程式,隨機發送點擊/滑動/切換事件10000次,( -v -v -v)表示信息日誌為最高級,然後列印的信息傳到F:\monkey_log\test1.txt里. 如下圖所示: 2.單元測試 2. ...
  • This interface shows how a spring animation can be created by specifying a “damping” (bounciness) and “response” (speed). 這個交互顯示瞭如何通過指定“阻尼”(有彈性)和“響應”( ...
  • 場景 實現效果如下 註: 博客: https://blog.csdn.net/badao_liumang_qizhi 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 新建Android項目,首先打開activity_main.xml 修改其為FrameLayout幀佈局管理 ...
  • gradle配置項 1. compileSdkVersion 用哪個 Android SDK 版本編譯你的應用。因此我們強烈推薦總是使用最新的 SDK 進行編譯。在現有代碼上使用新的編譯檢查可以獲得很多好處,避免新棄用的 API ,並且為使用新的 API 做好準備。 2. minSdkVersion ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...