用JavaScript中的示例数组every()方法

JavaScript every()方法 (JavaScript every() method)

every() method is used to check a condition on all array elements (or specified elements) and returns true if all elements match the condition and return false if any element does not match the condition.

每隔()方法被用来检查在所有的数组元素(或指定的元素),并返回true的条件,如果所有的元件匹配条件和返回false,如果任何元素不匹配的条件。

Syntax:

句法:

    array.every(function, [value]);

Parameters: A function name and an optional value to be tested with all elements.

参数:要与所有元素一起测试的功能名称和可选值 。

Ref: JS Array every() function

参考: JS Array every()函数

Return value: true or false

返回值: true或false

Example:

例:

    Input:
    var arr1 = [10, 20, 30, 40, 50];
    var arr2 = [10, 0, -20, 40, 50];

    //function to check elements are positive or not
    function isPositive(n){
        return n>=0;
    }
    
    Function call:
    arr1.every(isPositive);
    arr2.every(isPositive)

    Output:
    true
    false

JavaScript Code to check whether all array elements are positive or not using Array.every() method

JavaScript代码使用Array.every()方法检查所有数组元素是否为正

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		//function to check positive numbers
		function isPositive(n){
			return n>=0;
		}
		
		var arr1 = [10, 20, 30, 40, 50];
		var arr2 = [10, 0, -20, 40, 50];
		
		if(arr1.every(isPositive)==true)
			document.write("arr1 has all positive values<br>");
		else
			document.write("arr1 does not have all positive values<br>");

		if(arr2.every(isPositive)==true)
			document.write("arr2 has all positive values<br>");
		else
			document.write("arr2 does not have all positive values<br>");			
		
	</script>
</body>
</html>

Output

输出量

arr1 has all positive values
arr2 does not have all positive values

JavaScript Code to check whether all array elements are greater than 10 or not using Array.every() method

JavaScript代码使用Array.every()方法检查所有数组元素是否都大于10

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		//function to check positive numbers
		function isPositive(n, value2test){
			return n>=value2test;
		}
		
		var arr1 = [10, 20, 30, 40, 50];
		var arr2 = [10, 0, -20, 40, 50];
		
		if(arr1.every(isPositive, 10)==true)
			document.write("arr1 has all values which are >=10<br>");
		else
			document.write("arr1 does not have all values which are >=10<br>");

		if(arr2.every(isPositive, 10)==true)
			document.write("arr2 has all values which are >=10<br>");
		else
			document.write("arr2 does not have all values which are >=10<br>");			
		
	</script>
</body>
</html>

Output

输出量

arr1 has all values which are >=10
arr2 does not have all values which are >=10


翻译自: https://www.includehelp.com/code-snippets/array-every-method-with-example-in-javascript.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值