欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数组方法(every some filter includes find findIndex)

程序员文章站 2023-12-23 12:46:40
...
	every 		遍历数组每一项,每一项返回true,最终结果为true.有一项返回false,停止遍历,结果返回为false。不改变原数组。
	some 		判断数组中是否存在满足的条件,若存在返回true,不存在返回false
	filter		过滤掉数组中不满足条件的值,返回一个新数组,不改变原数组
	includes	判断数组中是否包含某一个元素,若包含返回true,否则返回true
	find		查找数组中符合条件的第一个元素并返回该元素
	findIndex	查找数组中符合条件的第一个元素的索引
	{
		let arr = [1,2,3,4];
		let arr1 = arr.every((item,index,arr)=>{	
		   return item > 0; 
		});		
		console.log(arr1);		//true
	}
	
	{
		let arr = ["z","g","j"];
		let arr1 = arr.some(function(m,index,arr){
			return m == "z"
		})
		console.log(arr1)		//true
	}
	
	{
		let arr = ["z","g","j"]
		let arr1 = arr.filter(function(m,index,arr){
			return m != "z"
		})
		console.log(arr1)		//["g", "j"]
	}
	
	
	{
		let arr = ["z","g","j"]
		let arr1 = arr.includes("z")
		console.log(arr1);		//true  
	}
	
	
	{
		let arr = [1,33,44,22,6,9]
		let arr1 = arr.find(v => v > 10)					
		console.log(arr1)      //33									
	}



	{													
		let arr = [1,33,44,22,6,9]
		let arr1 = arr.findIndex(v => v > 10)
		console.log(arr1)		//1
	}
相关标签: 数组 javascript

上一篇:

下一篇: