ES6?數(shù)組some()和every()的使用及說明
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 // 簡化方式 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)文章
JS?生態(tài)系統(tǒng)加速Polyfill函數(shù)使用實(shí)例探索
這篇文章主要介紹了JS?生態(tài)系統(tǒng)加速Polyfill函數(shù)使用實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01cocos2dx骨骼動(dòng)畫Armature源碼剖析(一)
cocos2dx中的骨骼動(dòng)畫在程序中使用非常方便,從編輯器(cocostudio或flash插件dragonBones)得到xml或json數(shù)據(jù),調(diào)用代碼就可以直接展示出動(dòng)畫效果,下面通過本篇文章給大家分享cocos2dx骨骼動(dòng)畫Armature源碼剖析,需要的朋友一起來學(xué)習(xí)吧。2015-09-09JS實(shí)現(xiàn)的點(diǎn)擊按鈕圖片上下滾動(dòng)效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)的點(diǎn)擊按鈕圖片上下滾動(dòng)效果,涉及javascript事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01前端項(xiàng)目中報(bào)錯(cuò)Uncaught?(in?promise)的解決方法
最近在做項(xiàng)目的時(shí)候控制臺(tái)報(bào)了一個(gè)錯(cuò)Uncaught(in promise) false,這篇文章主要給大家介紹了關(guān)于前端項(xiàng)目中報(bào)錯(cuò)Uncaught?(in?promise)的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04JavaScript中call和apply方法的區(qū)別實(shí)例分析
這篇文章主要介紹了JavaScript中call和apply方法的區(qū)別,結(jié)合實(shí)例形式分析call和apply方法的功能、原理及相關(guān)使用操作區(qū)別,需要的朋友可以參考下2018-08-08JavaScript定時(shí)器設(shè)置、使用與倒計(jì)時(shí)案例詳解
這篇文章主要介紹了JavaScript定時(shí)器設(shè)置、使用與倒計(jì)時(shí)案例,詳細(xì)分析了javascript定時(shí)器的設(shè)置、取消、循環(huán)調(diào)用并附帶一個(gè)倒計(jì)時(shí)功能應(yīng)用案例,需要的朋友可以參考下2019-07-07Javascript中的方法鏈(Method Chaining)介紹
這篇文章主要介紹了Javascript中的方法鏈(Method Chaining)介紹,本文講解了Javascript Method Chaining、Method Chaining 使用、Method Chaining VS prototype Chaining等內(nèi)容,需要的朋友可以參考下2015-03-03