JavaScript中數(shù)組超級好用的方法
一說到JavaScript的數(shù)組,大家基本都能馬上想起pop()、push()、shift()、unshift()、indexof()等等,今天給大家分享幾個開發(fā)中常用的JS數(shù)組方法。
1、filter()
- 語法:array.filter(function(currentValue,index,arr), thisValue)
- 參數(shù)說明:
- currentValue:當(dāng)前元素對象(必選)
- index:當(dāng)前元素的索引值(可選)
- arr:當(dāng)前元素屬于的數(shù)組對象(可選)
- thisValue:對象作為該執(zhí)行回調(diào)時使用,傳遞給函數(shù),用作 "this" 的值。
- 如果省略了 thisValue ,"this" 的值為 "undefined"(可選)
- //過濾年齡大于10的元素
- var ages = [5, 32, 7, 10, 33, 12, 40];
- var res = ages.filter(function (currentValue) {
- return currentValue > 10;
- })
- console.log(res.toString());
- //輸出結(jié)果:32,33,12,40
- //箭頭函數(shù)寫法
- var res1 = ages.filter(item => item > 10)
- console.log(res.toString());
- //輸出結(jié)果:32,33,12,40
2、forEach()
- 語法:array.forEach(function(currentValue, index, arr), thisValue)
- 參數(shù)用法同上
- //循環(huán)輸出每個參數(shù)
- var ages = [5, 32, 7, 10, 33, 12, 40];
- ages.forEach(function (currentValue, index) {
- console.log("參數(shù):" + currentValue + "索引:" + index);
- })
- //箭頭函數(shù)寫法
- ages.forEach((item, index) => {
- console.log("參數(shù):" + item + "索引:" + index);
- })
再看下面一段代碼:
- //把10修改成20
- var ages = [5, 32, 7, 10, 33, 12, 40];
- ages.forEach(function (currentValue, index) {
- if (currentValue === 10) {
- ages[index] = 20
- return
- }
- console.log(index);
- })
- console.log(ages);
我們在代碼中將10的值改成20后,加了一個return,但是運(yùn)行結(jié)果顯示還是打印了7次index的值,這就是forEach的一個缺點(diǎn),只有循環(huán)結(jié)束才能停止。那如何解決呢?
3、some()
- 語法:array.some(function(currentValue,index,arr),thisValue)
- 參數(shù)用法同上
- //把10修改成20
- var ages = [5, 32, 7, 10, 33, 12, 40];
- ages.some(function (currentValue, index) {
- if (currentValue === 10) {
- ages[index] = 20
- return true
- }
- console.log(index);
- })
- console.log(ages);
- //把10修改成20 箭頭函數(shù)
- var ages = [5, 32, 7, 10, 33, 12, 40];
- ages.some((item, index) => {
- if (item === 10) {
- ages[index] = 20
- return true
- }
- console.log(index);
- })
- console.log(ages);
上面的代碼中運(yùn)行結(jié)果只會打印三次index的值,通過some就可以完美解決forEach()的不足,開發(fā)過程中就看大家的需要就行選擇。
4、every()
- 語法:array.every(function(currentValue,index,arr), thisValue)
- 參數(shù)用法同上
- //判斷每個元素的值是否都大于4
- var ages = [5, 32, 7, 10, 33, 12, 40];
- var res = ages.some(function (currentValue) {
- return currentValue > 4
})
- console.log(res);
- //輸出:true
- //箭頭函數(shù)
- var res = ages.some(item => item > 4)
- console.log(res);
5、reduce()
- 語法:array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
- 參數(shù)說明:
- total:必需。初始值, 或者計(jì)算結(jié)束后的返回值。
- currentValue: 必需。當(dāng)前元素
- currentIndex:可選。當(dāng)前元素的索引
- arr:可選。當(dāng)前元素所屬的數(shù)組對象。
- initialValue:可選。傳遞給函數(shù)的初始值
- //計(jì)算所有元素的和
- var numbers = [15.5, 2.3, 1.1, 4.7];
- var res = numbers.reduce(function (total, currentValue) {
- return total += currentValue
- }, 0)
- console.log(res);
- //23.6
- //計(jì)算大于4的元素的和
- var result = numbers.filter(item => item > 4).reduce((total, item) => total += item, 0)
- console.log(result);
- //20.2
6、合并數(shù)組
- 用法:var arr = [...數(shù)組1,...數(shù)組2]
- 結(jié)果:將數(shù)組2的元素值拼接到數(shù)組1元素值后面
- var arr = [1, 2, 3]
- var arr2 = [4, 5, 6]
- var res = [...arr, ...arr2]
- console.log(res);
- //輸出結(jié)果:[1, 2, 3, 4, 5, 6]
- var res = [...arr2, ...arr]
- console.log(res);
- //輸出結(jié)果: [4, 5, 6, 1, 2, 3]
【編輯推薦】