Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does javascript determine whether a given value is in an array

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how javascript determines whether a given value is in the array, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.

Judgment method: 1, the use of includes () method, syntax "arr.includes ('value')", if in the array then return true;2, the use of "arr.indexOf (" value ")" or "arr.lastIndexOf" statement, if the return value is greater than 0 in the array.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Method 1: using the includes () method of the array

The includes () method is used to determine whether an array contains a specified value, and if it returns true, otherwise false.

Var fruits = ['Apple', 'banana', 'durian', 'orange', 'jackfruit', 'pear']; if (fruits.includes ('durian')) {console.log ("given value is in array");} else {console.log ("given value is not in array");}

Output result:

The given value is in the array

Method 2: use the indexOf () or lastIndexOf () method of the array

The indexOf () method returns the first occurrence of a specified element in the array. If the element to be retrieved does not appear, the method returns-1.

Implementation idea: use this method to check the first occurrence of the specified value in the array and, if the location exists, to include the given element. If-1 is returned, the given element is not included.

Var fruits = ['Apple', 'banana', 'durian', 'orange', 'jackfruit', 'pear']; var b = fruits.indexOf ("orange"); if (b > 0) {console.log ("given value is in array");} else {console.log ("given value is not in array");}

Output result:

The given value is in the array

The lastIndexOf () method searches for an element in an array and returns its last occurrence. If the element to be retrieved does not appear, the method returns-1.

Implementation idea: use this method to check the last occurrence of the specified value in the array and include the given element if the location exists; if-1 is returned, the given element is not included.

Var fruits = ['Apple', 'banana', 'durian', 'orange', 'jackfruit', 'pear']; var b = fruits.lastIndexOf ("grape"); if (b > 0) {console.log ("given value is in array");} else {console.log ("given value is not in array");}

Output result:

The given value is not in the array. These are all the contents of the article "how javascript determines whether a given value is in an array". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 280

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report