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

Javascript之遍歷數(shù)組方法

開發(fā) 前端
作為一個(gè)程序?qū)τ跀?shù)組遍歷大家都不是很陌生,再開發(fā)中我們也經(jīng)常要處理數(shù)組。這里我們討論下常用的數(shù)組遍歷方法和區(qū)別。

 [[271751]]

作為一個(gè)程序?qū)τ跀?shù)組遍歷大家都不是很陌生,再開發(fā)中我們也經(jīng)常要處理數(shù)組。這里我們討論下常用的數(shù)組遍歷方法和區(qū)別。

1.第一種for循環(huán)

  1. var arr = [1,2,3,4,5];  
  2. for(var i=0,i<arr.length;i++){  
  3.  console.log("for循環(huán):"+arr[i]); //打印出1,2,3,4,5  

2.for ..in 遍歷方式

  1. // for in 遍歷需要兩個(gè)形參 ,index表示數(shù)組的下標(biāo)(可以自定義),arr表示要遍歷的數(shù)組 
  2. var arr = [{num:1},{num:2},{num:3},{num:4},{num:5}]; 
  3.  
  4. for(var index arr){ 
  5.   
  6.  console.log("index:"+index,"num:"+arr[index].num);// 依次打印出 index:0,num:1... 
  7.   

3.forEach 遍歷方式

  1. var arr = [{num:1},{num:2},{num:3},{num:4},{num:5}]; 
  2.  
  3. arr.forEach(function(item,index){ 
  4.  
  5.                 console.log("index:"+index,"num:"+item.num); //同樣打印index:0,num:1... 
  6. }); 

4.map 遍歷方式

  1. var list = [1, 2, 3, 4, 5]; 
  2.  
  3. var arr = []; 
  4.  
  5. arr = list.map((value, index) => { 
  6. return value * 2; 
  7. }); 
  8.  
  9. console.log(arr);//[2, 4, 6, 8, 10] 
  10.  
  11. 這里map和forEach在遍歷數(shù)組的區(qū)別就是map 有返回值,返回一個(gè)新的數(shù)組,而forEach沒有返回值 

5.for-of遍歷方式

  1. var arr = [1,2,3,4,5]; 
  2.  
  3. for(var value of arr){ 
  4.   
  5.      console.log("數(shù)組數(shù)據(jù):"+value); //1,2,3,4,5 

在這還有一個(gè)要想說的就是,在使用遍歷數(shù)組時(shí)候,如果想跳出遍歷return true fasle break沒有反應(yīng),這其實(shí)是使用的遍歷方法不對,當(dāng)想跳出循環(huán)可以使用every 和 some方法。

  1. //every()當(dāng)內(nèi)部return false時(shí)跳出整個(gè)循環(huán)(return true;也要寫入) 
  2.  
  3. let list = [1, 2, 3, 4, 5]; 
  4.  
  5. list.every((value, index) => { 
  6.  if(value > 3){ 
  7.  console.log(value); // 4 
  8.  return false
  9.  }else
  10.  console.log(value); // 1 2 3 
  11.  return true
  12.  } 
  13. }); 
  14.  
  15.  
  16. ================================ 
  17.  
  18. //some 當(dāng)內(nèi)部return true時(shí)跳出整個(gè)循環(huán) 
  19.  
  20.  let list = [1, 2, 3, 4, 5]; 
  21.  
  22.  list.some((value, index) => { 
  23.  if(value === 3){ 
  24.  return true; //當(dāng)內(nèi)部return true時(shí)跳出整個(gè)循環(huán) 
  25.  } 
  26.  console.log(value)// 1 2  
  27.  }); 

 

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

2021-02-05 23:08:10

JS代碼循環(huán)

2009-11-17 15:00:19

PHP遍歷數(shù)組

2022-11-13 15:33:30

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

2021-03-29 12:01:00

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

2023-07-04 15:52:49

JavaScript數(shù)組

2022-11-23 16:12:57

JavaScript數(shù)據(jù)類型數(shù)組

2023-02-01 08:31:48

2023-11-14 16:57:10

2020-03-19 15:30:08

JavaScript數(shù)組字符串

2022-05-06 12:03:16

數(shù)組Javascript

2022-09-07 11:52:48

forforEach前端

2024-03-21 14:27:13

JavaScript數(shù)組

2024-08-23 15:34:23

JavaScrip數(shù)組

2025-02-19 12:00:00

JavaScript代碼數(shù)組方法

2022-04-28 08:41:53

JavaScript數(shù)組

2010-11-24 13:11:06

MySQL遍歷數(shù)據(jù)表

2020-06-30 10:37:55

JavaScript開發(fā)技術(shù)

2022-08-10 12:02:52

面試JavaScript

2016-10-08 21:25:36

Javascript數(shù)組Web

2022-07-06 10:04:45

JavaScript數(shù)組前端
點(diǎn)贊
收藏

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