js array high-order function - filter() method

filter() method

⭐In general, the filter() method is used to filter elements in an array and return a new array.
grammar:

array.filter(function(value, index, arr), thisValue)

Parameter Description:

  • function: function, which specifies the filter conditions. required. The function accepts three parameters: the value of the current element, the index of the current element, and the array containing the element.
  • thisValue: optional. The object to be used as the execution callback, passed to the function, and used as the value of "this". If thisValue is omitted, it defaults to the global object (window object in the browser environment).

Return Value:
Returns a new array containing the elements that meet the criteria. The following will give 5 examples. After understanding the examples, you can basically master them.

-------------------------------------------------------------⭐------------------------------------------------------------------

⭐⭐⭐ Example 1

Suppose we have an array of numbers, and now we want to filter out all numbers greater than or equal to 5:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
let result = numbers.filter(function(num) {
    
    
    return num >= 5;
});
console.log(result);   // 输出:[5, 6, 7, 8]

In the code above, the filter condition is defined using an anonymous function and passed as the first parameter to the filter() method. The final result is stored in the variable result and output to the console.
-------------------------------------------------- -----------⭐-------------------------------------- ----------------------------

⭐⭐⭐ Example 2

				let arr = [{
    
    
					id: 1,
					name: '神墓'
				}, {
    
    
					id: 2,
					name: '完美世界'
				}, {
    
    
					id: 1,
					name: '长生界'
				}, {
    
    
					id: 7,
					name: '遮天'
				}, {
    
    
					id: 1,
					name: '不死不灭'
				}]
				console.log(arr.filter(item => item.id == 1));
				

Print result:
insert image description here

In the above example, we filter out each item whose id is 1 in the array. There are SQL statements on the back end to query, and the front end can also implement query and screening in this way.

There are also other ways of writing, the array remains unchanged:

			let fe = arr.filter((item)=function (){
    
    
				return (item.id ==1 )
			})
			console.log(fe);

Written in this way, it is exactly the same as the result printed above.
-------------------------------------------------- -----------⭐-------------------------------------- ----------------------------

⭐⭐⭐ Example 3

			const words = ['赏', '花', '赏', '月', '赏', '秋','香'];
			const result = words.filter(word => word != '赏');
			console.log(result);

insert image description hereIn the above example, we picked out all the words in the array words that are not reward words

-------------------------------------------------------------⭐------------------------------------------------------------------

⭐⭐⭐ Example 4

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);

insert image description hereIn the above example, we filter the elements in the words array whose length is greater than 6, because each element is a word, a string, and the string also has a length, so this can also filter out what we want.

-------------------------------------------------------------⭐------------------------------------------------------------------

⭐⭐⭐ Example 5

			function isBigEnoughcount(value) {
    
    
				return value >= 10;
			}
			const filtered = [12, 5, 8, 130, 44].filter(isBigEnoughcount);
			console.log(filtered);

insert image description here
In this example, an array is declared and filtered, and the logic executed is a condition. The condition is that the value is greater than or equal to 10, that is to say, we declare an array, and filter the number of each item greater than 10 in the array.


Guess you like

Origin blog.csdn.net/dyk11111/article/details/131242109