Home  >  Article  >  Web Front-end  >  JS array learning returns all elements that meet the given conditions

JS array learning returns all elements that meet the given conditions

青灯夜游
青灯夜游Original
2021-08-27 15:28:468913browse

In the previous article "JS Array Learning: Determining whether all array elements meet the given conditions", we introduced several methods to detect whether all array elements meet the specified conditions. This time we continue to talk about array traversal and introduce the method of JavaScript returning elements that meet specified conditions in an array. Friends in need can learn about it~

The main content of today’s article is: traversing arrays and detecting arrays Whether the elements in the array satisfy the specified conditions, return the array elements that satisfy the conditions. To put it simply: it is to filter array elements based on specified conditions.

Let’s introduce two methods below, starting with the familiar for loop, and then introducing a built-in function-see how this function can filter array elements.

Method 1: Use for loop

Implementation idea: use for statement to traverse the array, and determine whether the array elements match in each loop Condition, if it is met, it will be output, if it is not met, it will jump out of this loop.

Let’s learn more about it through examples:

Example 1: Return all even numbers

var a = [2,3,4,5,6,7,8];
for(var i=0;i<a.length;i++){
	if (a[i] % 2 == 0) {
		console.log(a[i]);
	}else{
		continue;
	}
}

Output results:

JS array learning returns all elements that meet the given conditions

Example 2: Return all leap years

var a = [1995,1996,1997,1998,1999,2000,2004,2008,2010,2012,2020];
for(var i=0;i<a.length;i++){
	if(a[i]%4==0 && a[i]%100!=0){
			  console.log(a[i]);
	}
	else {
		continue;
	}
}

Output result:

JS array learning returns all elements that meet the given conditions

Method 2: Use the filter() method

The filter() method can return elements in the array that meet the specified conditions.

array.filter(function callbackfn(Value,index,array),thisValue)

function callbackfn(Value,index,array): A callback function, which cannot be omitted. It can accept up to three parameters:

  • value: The value of the current array element, cannot be omitted.

  • index: The numeric index of the current array element.

  • array: The array object to which the current element belongs.

The return value is a new array containing all the values ​​for which the callback function returns true. If the callback function returns false for all elements of array , the length of the new array is 0.

Let’s learn more about it through examples:

Example 1: Return all even numbers

var a = [2,3,4,5,6,7,8];
function f (value) {
	if (value % 2 == 0) {
		return true;
	}else{
		 return false;
	}
}
var b = a.filter(f);
console.log(b);

Output results:

JS array learning returns all elements that meet the given conditions

Example 2: Return all leap years

var a = [1995,1996,1997,1998,1999,2000,2004,2008,2010,2012,2020];
function f (value) {
	if(value%4==0 && value%100!=0){
		return true;
	}
	else {
		return false;
	}
}
var b = a.filter(f);
console.log(b);

Output result:

JS array learning returns all elements that meet the given conditions

Okay, let’s talk about it Here it is, you can watch it if you need it: javascript video tutorial

The above is the detailed content of JS array learning returns all elements that meet the given conditions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn