欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Javascript中的every()與some()的區(qū)別和應(yīng)用小結(jié)

 更新時(shí)間:2023年05月10日 14:53:55   作者:愛吃糖的小明  
every跟some不同點(diǎn)在于,every要判斷數(shù)組中是否每個(gè)元素都滿足條件,只有都滿足條件才返回true,只要有一個(gè)不滿足就返回false,這篇文章主要介紹了Javascript中的every()與some()的區(qū)別和應(yīng)用,需要的朋友可以參考下

Javascript中的every()與some()的區(qū)別和應(yīng)用

every()定義和用法 

every() 方法用于檢測(cè)數(shù)組所有元素是否都符合指定條件(通過函數(shù)提供)。

every() 方法使用指定函數(shù)檢測(cè)數(shù)組中的所有元素:

  • 如果數(shù)組中檢測(cè)到有一個(gè)元素不滿足,則整個(gè)表達(dá)式返回 false ,且剩余的元素不會(huì)再進(jìn)行檢測(cè)。
  • 如果所有元素都滿足條件,則返回 true。

注意: every() 不會(huì)對(duì)空數(shù)組進(jìn)行檢測(cè)。

注意: every() 不會(huì)改變?cè)紨?shù)組。

isEvery:function(){
				let a = [2,3,5,7,9]
				let status = a.every(function(currentValue,index,arr){
					console.log(currentValue) // 必須。當(dāng)前元素的值 
					console.log(index)        // 可選。當(dāng)前元素的索引值
					console.log(arr)          // 可選。當(dāng)前元素屬于的數(shù)組對(duì)象
					return currentValue >= 1  // 判斷數(shù)組中是否所有值都大于等于1
				})
				console.log(status)
			}
				 // currentValue:2
				 // index:0
				 // arr:2,3,5,7,9,
				 // currentValue:3
				 // index:1
				 // arr:2,3,5,7,9,
				 // currentValue:5
				 // index:2
				 // arr:2,3,5,7,9,
				 // currentValue:7
				 // index:3
				 // arr:2,3,5,7,9,
				 // currentValue:9
				 // index:4
				 // arr:2,3,5,7,9,
				 // status:true

 every()參數(shù)說明

function(currentValue, index,arr){
	// currentValue	必須。當(dāng)前元素的值
	// index	可選。當(dāng)前元素的索引值
	// arr	可選。當(dāng)前元素屬于的數(shù)組對(duì)象
    // 布爾值。如果所有元素都通過檢測(cè)返回 true,否則返回 false。
	}

some()定義和用法 

some() 方法用于檢測(cè)數(shù)組中的元素是否滿足指定條件(函數(shù)提供)。

some() 方法會(huì)依次執(zhí)行數(shù)組的每個(gè)元素:

  • 如果有一個(gè)元素滿足條件,則表達(dá)式返回true , 剩余的元素不會(huì)再執(zhí)行檢測(cè)。
  • 如果沒有滿足條件的元素,則返回false。

注意: some() 不會(huì)對(duì)空數(shù)組進(jìn)行檢測(cè)。

注意: some() 不會(huì)改變?cè)紨?shù)組。

isSome:function(){
				let a = [2,3,5,7,9]
				let status = a.some(function(currentValue,index,arr){
					console.log(currentValue) // 必須。當(dāng)前元素的值 
					console.log(index)        // 可選。當(dāng)前元素的索引值
					console.log(arr)          // 可選。當(dāng)前元素屬于的數(shù)組對(duì)象
					return currentValue >= 1  // 判斷數(shù)組中是否有值大于等于1
				})
				console.log(status)
			}
 // currentValue:2
 // index:0
 // arr:2,3,5,7,9
 // true

 some()參數(shù)說明 

function(currentValue, index,arr){
	// currentValue	必須。當(dāng)前元素的值
	// index	可選。當(dāng)前元素的索引值
	// arr	可選。當(dāng)前元素屬于的數(shù)組對(duì)象
    // 布爾值。如果所有元素都通過檢測(cè)返回 true,否則返回 false。
	}

every()和some()的主要區(qū)別

every()會(huì)對(duì)數(shù)組中的每一個(gè)元素,即currentValue進(jìn)行匹配,只有全部滿足條件才會(huì)返回true,而some()中,只需有一個(gè)currentValue滿足條件即可返回true,后面的currentValue不會(huì)再進(jìn)行比較

 every()和some()的應(yīng)用場(chǎng)景

如下圖,有一個(gè)表格,運(yùn)用every()和some()來判斷選中的表格狀態(tài),當(dāng)選中其中一條數(shù)據(jù)時(shí),判斷他的當(dāng)前狀態(tài),當(dāng)選擇多條數(shù)據(jù)時(shí)判斷他的當(dāng)前狀態(tài)

vue代碼片段

data() {
			return {
				statusDes:''
			}
		},
methods:{
			handleSelectionChange:function(val){
				const statusList = []  //定義數(shù)組,保存每一項(xiàng)狀態(tài)
				val.forEach((item) =>{
                    // 遍歷選擇的項(xiàng),把狀態(tài)保存到數(shù)組
					statusList.push(item.status)
				})
                // every()會(huì)遍歷每一個(gè)狀態(tài),當(dāng)全部滿足條件時(shí),才返回true
				this.statusDes = statusList.every(this.isAllQuery)
                // some()會(huì)遍歷狀態(tài),只要一個(gè)滿足,就返回true 
                // this.statusDes = statusList.some(this.isAllQuery)
			},
            // 過濾狀態(tài),即當(dāng)狀態(tài)顯示:”是“時(shí),返回true
			isAllQuery(status){
				return status === '是'
			},
			changes:function(){
				if(this.statusDes){
					this.$alert('是', '成功狀態(tài)', { confirmButtonText: '確定', type: 'success' })
				}else {
					this.$alert('否', '失敗狀態(tài)', { confirmButtonText: '確定', type: 'warning' })
				}
			},
			isTrue(value){
				return value >=1
			}
		}

結(jié)果分析

every()全選,發(fā)現(xiàn)狀態(tài)不同時(shí):

some()全選,發(fā)現(xiàn)狀態(tài)不同時(shí):

到此這篇關(guān)于Javascript中的every()與some()的區(qū)別和應(yīng)用的文章就介紹到這了,更多相關(guān)js every()與some()的區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論