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ù)組的所有成員是否滿足指定的測試
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ù)組新增的一些常用的方法
forEachmapfiltersomeeveryfindfindIndexfindLastfindLastIndexreduce
以上這些方法,用法都一樣,效果不同
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返回值為false5.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返回值為false6.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實(shí)現(xiàn)同一頁面多個(gè)運(yùn)動(dòng)效果的方法
這篇文章主要介紹了js實(shí)現(xiàn)同一頁面多個(gè)運(yùn)動(dòng)效果的方法,涉及javascript操作頁面元素運(yùn)動(dòng)效果的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Web純前端“旭日?qǐng)D”實(shí)現(xiàn)元素周期表
本文主要介紹了Web純前端“旭日?qǐng)D”實(shí)現(xiàn)元素周期表的實(shí)例解析。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
JS實(shí)現(xiàn)盒子跟著鼠標(biāo)移動(dòng)及鍵盤方向鍵控制盒子移動(dòng)效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)盒子跟著鼠標(biāo)移動(dòng)及鍵盤方向鍵控制盒子移動(dòng)效果,涉及javascript事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01
JS中的form.submit()不能提交表單的錯(cuò)誤原因
這篇文章主要介紹了JS中的form.submit()不能提交表單的錯(cuò)誤原因,本文最后得出結(jié)論是按鈕的ID、名稱不要使用submit,需要的朋友可以參考下2014-10-10
JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過顯示下拉框
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過顯示下拉框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
uniapp基礎(chǔ)篇之上傳圖片的實(shí)戰(zhàn)步驟
應(yīng)用uni-app開發(fā)跨平臺(tái)App項(xiàng)目時(shí),上傳圖片、文檔等資源功能需求十分常見,下面這篇文章主要給大家介紹了關(guān)于uniapp基礎(chǔ)篇之上傳圖片的相關(guān)資料,需要的朋友可以參考下2022-12-12
javascript漢字拼音互轉(zhuǎn)的簡單實(shí)例
下面小編就為大家?guī)硪黄猨avascript漢字拼音互轉(zhuǎn)的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10

