1. Five ways to determine whether a string contains another string
Methods of String objects
Method 1: indexOf() (Recommended)
var str = "123" (("2") != -1); // true
The indexOf() method returns the first occurrence of a specified string value in the string. If the string value to be retrieved does not appear, the method returns -1.
Method 2: match()
var str = "123" var reg = RegExp(/3/); if((reg)){ //Include;}
The match() method can retrieve the specified value within a string, or find a match for one or more regular expressions.
Method 3: search()
var str = "123" (("2") != -1); // true
The search() method is used to retrieve the specified substring in a string, or to retrieve the substring that matches the regular expression. If no matching substring is found, return -1.
Methods of RegExp Objects
Method 4: test()
var str = "123" var reg = RegExp(/3/); ((str) != -1); // true
The test() method is used to retrieve the specified value in the string. Returns true or false.
Method 5:exec()
var str = "123" var reg = RegExp(/3/); if((str)){ //Include;}
exec() methodUsed to retrieve matches of regular expressions in strings. Returns an array where matching results are stored. If no match is found, the return value is null.
2. JavaScript determines whether the array contains
In JavaScript, it is very common to determine whether an array contains an element. For different needs, we can use different ways to determine whether an array contains an element. This article will introduce several common ways of judgment.
Method 1: Use include() method
In ES6, an include() method is provided for the array to determine whether the array contains an element. Its syntax is as follows:
(searchElement[, fromIndex])
Where, searchElement represents the element to be searched, fromIndex represents the starting position of the search, and the default value is 0.
The include() method returns a Boolean value indicating whether the specified element is included in the array.
The following is an example code to use the include() method to determine whether an array contains an element:
const fruits = ['apple', 'banana', 'orange']; (('apple')); // true (('banana')); // true (('pear')); // false
Method 2: Use indexOf() method
If your code needs to be ES5 or earlier, you can use the indexOf() method to determine whether the array contains an element. Its syntax is as follows:
(searchElement[, fromIndex])
Where, searchElement represents the element to be searched, fromIndex represents the starting position of the search, and the default value is 0.
The indexOf() method returns a number indicating the location of the specified element for the first time in the array. If the element is not contained in the array, return -1.
The following is an example code to use the indexOf() method to determine whether an array contains an element:
const fruits = ['apple', 'banana', 'orange']; (('apple') !== -1); // true (('banana') !== -1); // true (('pear') !== -1); // false
Method 3: Use the find() method
ES6 also provides a find() method, which can be used to get the first element in an array that meets the criteria. If there is no element that meets the criteria in the array, undefined is returned. We can use this to determine whether an array contains an element. The following is an example code to use the find() method to determine whether an array contains an element:
const fruits = ['apple', 'banana', 'orange']; ((item => item === 'apple') !== undefined); // true ((item => item === 'banana') !== undefined); // true ((item => item === 'pear') !== undefined); // false
The find() method here uses an arrow function, which accepts an argument item, representing each element in the array. The return value of the arrow function is whether item is equal to the element to be looked for, if so, return true, otherwise return false. Finally, the find() method returns a search for whether the result is undefined, that is, whether there are elements that meet the criteria.
Method 4: Use some() method
Similar to the find() method, ES6 also provides a some() method, which can be used to determine whether there are elements in the array that meet the criteria. Return true if there are elements in the array that meet the criteria, otherwise false. The following is an example code to use the some() method to determine whether an array contains an element:
const fruits = ['apple', 'banana', 'orange']; ((item => item === 'apple')); // true ((item => item === 'banana')); // true ((item => item === 'pear')); // false
The some() method here uses an arrow function, which accepts an argument item, representing each element in the array. The return value of the arrow function is whether item is equal to the element to be looked for, if so, return true, otherwise return false. Finally, the some() method returns a lookup if the result is true, that is, whether there are elements that meet the criteria.
Summarize
This article introduces several common ways to determine whether an array contains an element, which is:
- include() method
- indexOf() method
- find() method
- some() method
You can choose one of these according to your needs. If you are using ES6 or above, it is recommended to use the include() method or find() method, which is more intuitive and convenient; if you need to be compatible with ES5 or earlier, you can use the indexOf() method or some() method.
This is the article about judging whether a string, array, etc. in JavaScript contains a certain value. For more related JS content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!