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

JavaScript中數(shù)組超級好用的方法

開發(fā) 前端
一說到JavaScript的數(shù)組,大家基本都能馬上想起pop()、push()、shift()、unshift()、indexof()等等,今天給大家分享幾個開發(fā)中常用的js數(shù)組方法。

一說到JavaScript的數(shù)組,大家基本都能馬上想起pop()、push()、shift()、unshift()、indexof()等等,今天給大家分享幾個開發(fā)中常用的JS數(shù)組方法。

[[425130]]

1、filter() 

  1. 語法:array.filter(function(currentValue,index,arr), thisValue) 
  2. 參數(shù)說明: 
  3. currentValue:當(dāng)前元素對象(必選) 
  4. index:當(dāng)前元素的索引值(可選) 
  5. arr:當(dāng)前元素屬于的數(shù)組對象(可選) 
  6. thisValue:對象作為該執(zhí)行回調(diào)時使用,傳遞給函數(shù),用作 "this" 的值。 
  7. 如果省略了 thisValue ,"this" 的值為 "undefined"(可選) 

  

  1. //過濾年齡大于10的元素 
  2. var ages = [5, 32, 7, 10, 33, 12, 40]; 
  3. var res = ages.filter(function (currentValue) { 
  4.   return currentValue > 10; 
  5. }) 
  6. console.log(res.toString()); 
  7. //輸出結(jié)果:32,33,12,40 
  8.  
  9. //箭頭函數(shù)寫法 
  10. var res1 = ages.filter(item => item > 10) 
  11. console.log(res.toString()); 
  12. //輸出結(jié)果:32,33,12,40 

2、forEach() 

  1. 語法:array.forEach(function(currentValue, index, arr), thisValue) 
  2. 參數(shù)用法同上 

  

  1. //循環(huán)輸出每個參數(shù) 
  2. var ages = [5, 32, 7, 10, 33, 12, 40]; 
  3. ages.forEach(function (currentValue, index) { 
  4.   console.log("參數(shù):" + currentValue + "索引:" + index); 
  5. }) 
  6.  
  7.  
  8. //箭頭函數(shù)寫法 
  9. ages.forEach((item, index) => { 
  10.   console.log("參數(shù):" + item + "索引:" + index); 
  11. }) 

再看下面一段代碼: 

  1. //把10修改成20 
  2. var ages = [5, 32, 7, 10, 33, 12, 40]; 
  3. ages.forEach(function (currentValue, index) { 
  4.   if (currentValue === 10) { 
  5.     ages[index] = 20 
  6.     return 
  7.   } 
  8.   console.log(index); 
  9. }) 
  10.  
  11. console.log(ages); 

我們在代碼中將10的值改成20后,加了一個return,但是運(yùn)行結(jié)果顯示還是打印了7次index的值,這就是forEach的一個缺點(diǎn),只有循環(huán)結(jié)束才能停止。那如何解決呢?

3、some() 

  1. 語法:array.some(function(currentValue,index,arr),thisValue) 
  2. 參數(shù)用法同上 

  

  1. //把10修改成20 
  2. var ages = [5, 32, 7, 10, 33, 12, 40]; 
  3. ages.some(function (currentValue, index) { 
  4.   if (currentValue === 10) { 
  5.     ages[index] = 20 
  6.     return true 
  7.   } 
  8.   console.log(index); 
  9. }) 
  10.  
  11. console.log(ages); 
  12.  
  13. //把10修改成20 箭頭函數(shù) 
  14. var ages = [5, 32, 7, 10, 33, 12, 40]; 
  15. ages.some((item, index) => { 
  16.   if (item === 10) { 
  17.     ages[index] = 20 
  18.     return true 
  19.   } 
  20.   console.log(index); 
  21. }) 
  22.  
  23. console.log(ages); 

上面的代碼中運(yùn)行結(jié)果只會打印三次index的值,通過some就可以完美解決forEach()的不足,開發(fā)過程中就看大家的需要就行選擇。

4、every() 

  1. 語法:array.every(function(currentValue,index,arr), thisValue) 
  2. 參數(shù)用法同上 

  

  1. //判斷每個元素的值是否都大于4 
  2. var ages = [5, 32, 7, 10, 33, 12, 40]; 
  3.  
  4.  
  5. var res = ages.some(function (currentValue) { 
  6.   return currentValue > 4 
  7. }) 

  8. console.log(res); 
  9. //輸出:true 
  10.  
  11. //箭頭函數(shù) 
  12. var res = ages.some(item => item > 4) 
  13. console.log(res); 

5、reduce() 

  1. 語法:array.reduce(function(total, currentValue, currentIndex, arr), initialValue) 
  2. 參數(shù)說明: 
  3. total:必需。初始值, 或者計(jì)算結(jié)束后的返回值。 
  4. currentValue:   必需。當(dāng)前元素 
  5. currentIndex:可選。當(dāng)前元素的索引 
  6. arr:可選。當(dāng)前元素所屬的數(shù)組對象。 
  7. initialValue:可選。傳遞給函數(shù)的初始值 
  1. //計(jì)算所有元素的和 
  2. var numbers = [15.5, 2.3, 1.1, 4.7]; 
  3. var res = numbers.reduce(function (total, currentValue) { 
  4.   return total += currentValue 
  5. }, 0) 
  6.  
  7. console.log(res); 
  8. //23.6 
  9.  
  10. //計(jì)算大于4的元素的和 
  11. var result = numbers.filter(item => item > 4).reduce((total, item) => total += item, 0) 
  12. console.log(result); 
  13. //20.2 

6、合并數(shù)組 

  1. 用法:var arr = [...數(shù)組1,...數(shù)組2] 
  2. 結(jié)果:將數(shù)組2的元素值拼接到數(shù)組1元素值后面 

 

  1. var arr = [1, 2, 3] 
  2. var arr2 = [4, 5, 6] 
  3.  
  4. var res = [...arr, ...arr2] 
  5. console.log(res); 
  6. //輸出結(jié)果:[1, 2, 3, 4, 5, 6] 
  7.  
  8. var res = [...arr2, ...arr] 
  9. console.log(res); 
  10. //輸出結(jié)果: [4, 5, 6, 1, 2, 3] 

 【編輯推薦】

 

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

2023-08-18 15:12:00

JavaScript開發(fā)

2016-10-13 19:33:10

javascript數(shù)組indexOf

2016-12-27 10:19:42

JavaScriptindexOf

2015-07-16 14:51:13

下載助手斷點(diǎn)續(xù)傳多任務(wù)

2013-05-27 15:07:36

Eclipse插件

2020-06-24 07:44:12

Python數(shù)據(jù)技術(shù)

2024-07-26 00:35:33

2024-03-21 14:27:13

JavaScript數(shù)組

2022-11-13 15:33:30

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

2025-02-10 07:00:00

JavaScript數(shù)組方法前端

2021-08-27 13:20:06

PythonAddict模塊

2022-10-26 10:15:53

GoFramePHP數(shù)組

2021-02-07 22:59:55

JavaScript編程方法鏈

2021-04-26 07:51:00

JavaScript方法函數(shù)

2020-06-30 10:37:55

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

2023-02-01 08:31:48

2023-07-04 15:52:49

JavaScript數(shù)組

2019-07-25 10:08:05

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

2023-07-18 07:56:31

工具reduce業(yè)務(wù)

2024-06-18 10:28:46

點(diǎn)贊
收藏

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