js array high-order function - includes method
foreword
⭐JS array is a special object, which is characterized by storing values in order in the list of values. JS arrays are used to store data arranged in index order, such as numbers, strings, and objects. An array is a higher-order object similar to a list that can store any type of data and has some very useful methods. Among them, includes()
method is one of the most commonly used.
In JS, an array is a sequence of values []
enclosed . Arrays can contain values of different types such as numbers, strings, objects, etc. Here are some basic JS array operations:
General operations on arrays
create array
let myArray = []; // 创建一个空数组
let myArray = [1, 2, 3]; // 创建一个包含三个数值的数组
let myArray = new Array(); // 用构造函数创建一个空数组
let myArray = new Array(1, 2, 3); // 用构造函数创建一个包含三个数值的数组
Get the length of the array
let myArray = [1, 2, 3];console.log(myArray.length); // 输出 3
Access (traverse) array elements
let myArray = [1, 2, 3];
console.log(myArray[0]); // 输出 1
console.log(myArray[1]); // 输出 2
console.log(myArray[2]); // 输出 3
Modify array elements
let myArray = [1, 2, 3];
myArray[1] = 4;
console.log(myArray); // 输出 [1, 4, 3]
delete array element
let myArray = [1, 2, 3];
delete myArray[1];
console.log(myArray); // 输出 [1, 3]
append at the end of the array
let myArray = [1, 2, 3];
myArray.push(4);
console.log(myArray); // 输出 [1, 2, 3, 4]
Delete from the end of the array
let myArray = [1, 2, 3];
myArray.pop();
console.log(myArray); // 输出 [1, 2]
includes() method
⭐For whether an element is contained in the array, we can use includes()
the method . includes()
The method is used to determine whether a specified element is contained in the array, and if so, the method returns true, otherwise it returns false.
Following is the syntax of includes()
the method :
array.includes(searchElement[, fromIndex])
in:
searchElement
: The element to be searched, required.fromIndex
: Optional, start looking for elements at this index. If this parameter is omitted, the search starts from the beginning of the array. Returns false iffromIndex
is greater than or equal to the array length.
for example
When we use the includes() method, we can pass in an element as a parameter to determine whether the element is included in the array.
⭐⭐⭐The first example: determine whether an array contains a certain number
const numbers = [1, 2, 3, 4, 5];
console.log(numbers.includes(3));
console.log(numbers.includes(6));
The array numbers contains the number 3, so includes(3) returns true. The array numbers does not contain the number 6, so includes(6) returns false.
⭐⭐⭐Example 2: Determine whether an array contains a certain string
const fruits = ['长生界', '神墓', '遮天'];
console.log(fruits.includes('遮天'));
console.log(fruits.includes('完美世界'));
The array fruits contains the string ' shading ', so includes('banana')
returns true. And the array fruits does not contain the string ' grape ', so includes('grape')
false is returned.
⭐⭐⭐The third example: determine whether an object is contained in the array
const users = [
{
name: '叶天帝', age: 225 },
{
name: '石昊', age: 130 },
{
name: '辰南', age: 135 }
];
const user = {
name: '石昊', age: 30 };
console.log(users.includes(user)); // true
The array users contains an object equal to the user object, so includes(user) returns true.
key point
The first point: includes()
the method uses strict equality (===) when comparing elements.
Second point: If there are multiple identical elements in the array, the includes() method will only return the first matched element.
The third point: Compatibility:
includes()
The method is not compatible with all browsers. If you need to use the method in browsers that do not support includes()
the method
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
'use strict';
var O = Object(this);
var len = parseInt(O.length) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1]) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {
k = 0;
}
}
while (k < len) {
var currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) {
return true;
}
k++;
}
return false;
};
}
In this polyfill, Array.prototype.includes()
the method first gets the length of the array through the object and uses a while loop to iterate through the array. Returns true if the searched element exists, false otherwise.