ES6?數(shù)組some()和every()的使用及說明
ES6 數(shù)組some()和every()使用
some 英語翻譯為一些,every翻譯為所有,每個,所以some方法 只要其中一個為true 就會返回true的,相反,every()方法必須所有都返回true才會返回true,哪怕有一個false,就會返回false;
every()和 some()目的:確定數(shù)組的所有成員是否滿足指定的測試
every:一假即假some:一真即真
/**
* 計算對象數(shù)組中每個電腦的扣件系統(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;/**
* 假定有一個注冊頁面,判斷所有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())//只要一個為空就為false
// 簡化方式
var fields = [username, telephone,password];
console.log(fields)
var formIsValid = fields.every(function(field){
return field.validate()
});
console.log(formIsValid)
if(formIsValid){
//注冊成功
}else{
//給用戶一個錯誤提醒
}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){
//在這里進行相關(guān)操作
})2.map
返回值是一個新的 數(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ù)組中的元素,返回符合要求的元素。返回值是一個新的數(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 bigPotatos=potatos.filter(potato=>potato.weight>=100)
//potato.weight>=100 就是返回值,為布爾值,如果為true,則當前遍歷到potato就會作為新數(shù)組中的值4.some
此方法返回值是布爾值,判斷數(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 hasBig = potatos.some(potato => potato.weight >= 100)
// 只要返回值為true,則內(nèi)部停止遍歷,some返回值true,如果每次都返回false,則some返回值為false5.every
返回值是布爾值,判斷數(shù)組中的值是否都符合條件,如果是則返回true,有一個不符合則返回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
返回值為符合條件的對應(yīng)的那個值
后者從后往前遍歷
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)?
// 得到第一個符合條件的數(shù)據(jù),返回給變量7.findIndex 、findLastIndex
返回值為符合條件的對應(yīng)的那個值的下標
后者從后往前遍歷
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)?
// 得到第一個符合條件的數(shù)據(jù)的下標,返回給變量總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JS實現(xiàn)盒子跟著鼠標移動及鍵盤方向鍵控制盒子移動效果示例
這篇文章主要介紹了JS實現(xiàn)盒子跟著鼠標移動及鍵盤方向鍵控制盒子移動效果,涉及javascript事件響應(yīng)及頁面元素屬性動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-01-01
JavaScript實現(xiàn)鼠標經(jīng)過顯示下拉框
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)鼠標經(jīng)過顯示下拉框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04
uniapp基礎(chǔ)篇之上傳圖片的實戰(zhàn)步驟
應(yīng)用uni-app開發(fā)跨平臺App項目時,上傳圖片、文檔等資源功能需求十分常見,下面這篇文章主要給大家介紹了關(guān)于uniapp基礎(chǔ)篇之上傳圖片的相關(guān)資料,需要的朋友可以參考下2022-12-12

