最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ES6?數(shù)組some()和every()的使用及說明

 更新時(shí)間:2023年01月16日 09:54:36   作者:周家大小姐.  
這篇文章主要介紹了ES6?數(shù)組some()和every()的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

ES6 數(shù)組some()和every()使用

some 英語翻譯為一些,every翻譯為所有,每個(gè),所以some方法 只要其中一個(gè)為true 就會(huì)返回true的,相反,every()方法必須所有都返回true才會(huì)返回true,哪怕有一個(gè)false,就會(huì)返回false;

every()和 some()目的:確定數(shù)組的所有成員是否滿足指定的測(cè)試

  • every:一假即假
  • some:一真即真 
/** 
 * 計(jì)算對(duì)象數(shù)組中每個(gè)電腦的扣件系統(tǒng)是否可用,大于16位操作系統(tǒng)表示可用,否則不可用
*/
var computers = [
    {name:"Apple",ram:8},
    {name:"IBM",ram:4},
    {name:"Acer",ram:32},
];
 var result= computers.every(function(computer){
   return computer.ram > 16
})
console.log(result)//false;
var some = computers.some(function(computer){
   return computer.ram > 16
})
console.log(some)//true;
/**
 * 假定有一個(gè)注冊(cè)頁面,判斷所有Input內(nèi)容的長度是否大于0
 * 
 */
function Field(value){
    this.value = value
}
// 在原型上定義方法
Field.prototype.validate = function(){
    return this.value.length > 0;
}
var username = new Field('2131');
var telephone  = new Field('8888888888888')
console.log(username.validate() && telephone.validate())//true
 
 
//二`:
var username = new Field('2131');
var telephone  = new Field('8888888888888')
let password  = new Field('');
//console.log(username.validate() && telephone.validate())//只要一個(gè)為空就為false
// 簡(jiǎn)化方式
var fields = [username, telephone,password];
console.log(fields)
var formIsValid = fields.every(function(field){
    return field.validate()
});
console.log(formIsValid)
 
if(formIsValid){
    //注冊(cè)成功
}else{
    //給用戶一個(gè)錯(cuò)誤提醒
}

ES6數(shù)組新增方法

ES6數(shù)組新增的一些常用的方法

  • forEach
  • map
  • filter
  • some
  • every
  • find
  • findIndex
  • findLast
  • findLastIndex
  • reduce

以上這些方法,用法都一樣,效果不同

arr.方法名((item,index,arr)=>{
})

1. forEach

此方法是用來代替 for 循環(huán)遍歷數(shù)組

let arr=[1,2,3,4];
arr.forEach(function(value,index,arr){
//在這里進(jìn)行相關(guān)操作
})

2.map

返回值是一個(gè)新的 數(shù)組,數(shù)組長度和原數(shù)組相同,數(shù)組中的值,就是函數(shù)中的返回值。

let potatos = [
? { id: '1001', weight: 50 },
? { id: '1002', weight: 80 },
? { id: '1003', weight: 120 },
? { id: '1004', weight: 40 },
? { id: '1005', weight: 110 },
? { id: '1006', weight: 60 }
]
? let w = potatos.map(function(potato) {
? ? ? ? return potato.weight
? ? })
?//在這里,potato.weight就是函數(shù)的返回值

3.filter

此方法是依次拿出數(shù)組中的元素,返回符合要求的元素。返回值是一個(gè)新的數(shù)組,數(shù)組中的值是符合條件的值,而這個(gè)條件是函數(shù)的返回值。

let potatos = [
? { id: '1001', weight: 50 },
? { id: '1002', weight: 80 },
? { id: '1003', weight: 120 },
? { id: '1004', weight: 40 },
? { id: '1005', weight: 110 },
? { id: '1006', weight: 60 }
]
let bigPotatos=potatos.filter(potato=>potato.weight>=100)
//potato.weight>=100 就是返回值,為布爾值,如果為true,則當(dāng)前遍歷到potato就會(huì)作為新數(shù)組中的值

4.some

此方法返回值是布爾值,判斷數(shù)組中是否有符合條件的值,而這個(gè)條件是函數(shù)的返回值

let potatos = [
? { id: '1001', weight: 50 },
? { id: '1002', weight: 80 },
? { id: '1003', weight: 120 },
? { id: '1004', weight: 40 },
? { id: '1005', weight: 110 },
? { id: '1006', weight: 60 }
]

let hasBig = potatos.some(potato => potato.weight >= 100)
// 只要返回值為true,則內(nèi)部停止遍歷,some返回值true,如果每次都返回false,則some返回值為false

5.every

返回值是布爾值,判斷數(shù)組中的值是否都符合條件,如果是則返回true,有一個(gè)不符合則返回false

let potatos = [
? { id: '1001', weight: 50 },
? { id: '1002', weight: 80 },
? { id: '1003', weight: 120 },
? { id: '1004', weight: 40 },
? { id: '1005', weight: 110 },
? { id: '1006', weight: 60 }
]

let hasBig = potatos.every(potato => potato.weight >= 100)
// 只要所有返回值為true,則every返回true,如果由一次返回false,則every返回值為false

6.find 、findLast

返回值為符合條件的對(duì)應(yīng)的那個(gè)值

后者從后往前遍歷

let potatos = [
? { id: '1001', weight: 50 },
? { id: '1002', weight: 80 },
? { id: '1003', weight: 120 },
? { id: '1004', weight: 40 },
? { id: '1005', weight: 110 },
? { id: '1006', weight: 60 }
]

let bigPotato = potatos.find(potato => potato.weight >= 100)?
// 得到第一個(gè)符合條件的數(shù)據(jù),返回給變量

7.findIndex 、findLastIndex

返回值為符合條件的對(duì)應(yīng)的那個(gè)值的下標(biāo)

后者從后往前遍歷

let potatos = [
? { id: '1001', weight: 50 },
? { id: '1002', weight: 80 },
? { id: '1003', weight: 120 },
? { id: '1004', weight: 40 },
? { id: '1005', weight: 110 },
? { id: '1006', weight: 60 }
]

let bigPotato = potatos.findIndex(potato => potato.weight >= 100)?
// 得到第一個(gè)符合條件的數(shù)據(jù)的下標(biāo),返回給變量

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

通渭县| 浮山县| 远安县| 顺平县| 化州市| 从化市| 云阳县| 清涧县| 明星| 东乡族自治县| 丰顺县| 横山县| 东阿县| 伊通| 绥棱县| 合肥市| 阿克苏市| 攀枝花市| 通州区| 大连市| 泗洪县| 专栏| 乡宁县| 永济市| 洛南县| 吴堡县| 沐川县| 宜宾市| 安福县| 泸西县| 祁东县| 顺义区| 高尔夫| 泰顺县| 红河县| 黑龙江省| 南部县| 汕头市| 洛浦县| 嘉荫县| 九寨沟县|