自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

簡單實用,JavaScript 的 8 個數(shù)組遍歷方法

開發(fā) 前端
在 JavaScript 中,有一些用于遍歷數(shù)組的方法。經(jīng)常的總結(jié)并記住它們,可以讓我們的工作得心應(yīng)手。

 引言

在 JavaScript 中,有一些用于遍歷數(shù)組的方法。經(jīng)常的總結(jié)并記住它們,可以讓我們的工作得心應(yīng)手。

 

[[331711]]

 

map

map() 數(shù)組的每個元素都會調(diào)用回調(diào)函數(shù),并將處理結(jié)果返回一個新數(shù)組。

 

  1. const numbers = [1, 2, 3, 4]; 
  2. const foo = number => number + 10; 
  3. const newNumbers = numbers.map(foo); 
  4. console.log(`新數(shù)組:${newNumbers}`); 
  5. console.log(`舊數(shù)組:${numbers}`); 
  6. /* 
  7.  * 新數(shù)組:11,12,13,14 
  8.  * 舊數(shù)組:1,2,3,4 
  9. */ 

every

every() 方法使用指定函數(shù)檢測數(shù)組中的所有元素是否滿足條件,元素全部滿足條件,方法返回 true ,有一個元素不滿足條件,方法返回 false 且其余元素不再檢測。

 

  1. const numbers = [1,2,3,4]; 
  2. const foo = num => num < 5; 
  3.  
  4. if (numbers.every(foo)) { 
  5.   console.log('數(shù)組中所有元素都小于 5'); // is ok 
  6. else { 
  7.   console.log('數(shù)組中至少有一個元素大于 5'); 

some

some() 方法使用指定函數(shù)檢測數(shù)組中是否有元素滿足條件,有一個元素滿足條件,方法返回 true 且剩余的元素不會再執(zhí)行檢測,沒有滿足條件的元素,方法返回 false 。

 

  1. const numbers = [1,2,3,4]; 
  2. const foo = num => num > 3; 
  3.  
  4. if (numbers.some(foo)) { 
  5.   console.log('數(shù)組中至少有一個元素值大于 3'); // is ok  
  6. else { 
  7.   console.log('數(shù)組中沒有大于 3 的元素值'); 

filter

filter() 方法通過一個函數(shù),篩選數(shù)組中的元素。用符合條件的元素創(chuàng)建一個新數(shù)組。

 

  1. const numbers = [1,2,3,4]; 
  2. const foo = number => number > 2; 
  3. const newNumbers = numbers.filter(foo); 
  4. console.log(`原始數(shù)組 [${numbers}] 中,滿足 > 2 的元素有 : ${newNumbers}`); 
  5. // 原始數(shù)組 [1,2,3,4] 中,滿足 > 2 的元素有 : 3,4 

reduce

reduce() 方法接收一個函數(shù)累加器,數(shù)組中的每個元素 (從左到右) 應(yīng)用于函數(shù),最終計算出一個最終值。

 

  1. const numbers = [1, 2, 3, 4]; 
  2. const sum = (total, num) => total + num; 
  3. const numbers_sum = numbers.reduce(sum, 0); // 將 0 作為 reduce 的初始值 
  4. console.log(`原始數(shù)組 '${numbers}' 的元素累加后,最終值是 ${numbers_sum}`); 
  5. // 原始數(shù)組 [1,2,3,4] 的元素累加后,最終值是 10 

reduceRight() 和 reduce() 使用方法一樣,區(qū)別是它從右到左將數(shù)組中的每個元素應(yīng)用于函數(shù)。

for

傳統(tǒng)的 for 循環(huán)遍歷數(shù)組很常用。

 

  1. const numbers = [1, 2, 3, 4]; 
  2. for (let index = 0; index < numbers.length; index++) { 
  3.   console.log(numbers[index]); // 1 2 3 4 

forEach

forEach() 將數(shù)組的每個元素傳入回調(diào)函數(shù),且對空數(shù)組不會執(zhí)行回調(diào)函數(shù)。

 

  1. const numbers = [1, 2, 3, 4]; 
  2. numbers.forEach((number, index, numbers) => { 
  3.   console.log(`下標(biāo) ${index} 在數(shù)組 ${numbers} 中的值是 ${number}`); 
  4. }); 
  5. /* 
  6.  * 下標(biāo) 0 在數(shù)組 1,2,3,4 中的值是 1 
  7.  * 下標(biāo) 1 在數(shù)組 1,2,3,4 中的值是 2 
  8.  * 下標(biāo) 2 在數(shù)組 1,2,3,4 中的值是 3 
  9.  * 下標(biāo) 3 在數(shù)組 1,2,3,4 中的值是 4 
  10. */ 

while

while 也可以遍歷數(shù)組,但很少用。

 

  1. let index = 0; 
  2. const numbers = [1,2,3,4]; 
  3. while(index < numbers.length) { 
  4.   console.log(numbers[index]); 
  5.   index ++; 
  6. // 1 2 3 4 

 

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2019-07-25 10:08:05

JavaScript數(shù)組轉(zhuǎn)換

2009-09-08 09:59:26

LINQ遍歷多個數(shù)組

2023-05-04 23:54:02

JavaScrip代碼技巧

2024-01-04 16:46:58

JavaScript開發(fā)

2021-06-18 10:05:14

JavaScript數(shù)組遍歷

2021-06-15 10:01:27

JavaScript數(shù)組遍歷Entries

2016-10-13 19:33:10

javascript數(shù)組indexOf

2024-04-25 07:54:46

遍歷數(shù)組PythonFor循環(huán)

2022-08-16 10:53:56

JavaScript前端技巧

2010-09-28 14:35:34

DOM遍歷

2016-12-27 10:19:42

JavaScriptindexOf

2021-09-22 23:17:09

Java開發(fā)數(shù)組

2025-02-17 11:10:49

2010-09-14 10:29:43

配置tftp服務(wù)器

2010-01-12 16:30:21

VB.NET數(shù)據(jù)轉(zhuǎn)換

2020-04-01 11:05:24

Spark數(shù)據(jù)傾斜Hadoop

2009-11-17 15:00:19

PHP遍歷數(shù)組

2022-11-13 15:33:30

JavaScript數(shù)組開發(fā)

2024-01-31 12:13:02

JavaScriptSet元素

2009-09-08 10:37:57

C#遍歷CheckBo
點贊
收藏

51CTO技術(shù)棧公眾號