In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "javascript how to judge whether the value is in the array", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to determine whether the value is in the array" this article.
Methods: 1, use the for statement to traverse the array, compare the array elements with the specified value, and the equality exists; 2, use the indexOf method, return the subscript value if the element exists in the array, and return "- 1" if it does not exist; 3, use the inArray () method to return the subscript value if the element exists, and return "- 1" if it does not exist.
The operating environment of this tutorial: windows7 system, javascript1.8.5&&jquery1.10.2 version, Dell G3 computer.
There is an array in js. How to determine whether an element exists in this array is determined by loop first. The code is as follows:
Var arr = ['axiajiaozhuo']; console.info (isInArray (arr,'a')); / / Loop method / * use loop method to determine whether an element exists in an array * @ param {Object} arr array * @ param {Object} value element value * / function isInArray (arr,value) {for (var I = 0; I)
< arr.length; i++){ if(value === arr[i]){ return true; } } return false;} 这种方式是比较通用的一种方式,但是需要自己写函数,下面看一下第二种方式: var arr = ['a','s','d','f'];console.info(arr.indexOf('a'));//在IE某些版本中不支持,可以自行扩展 这种方式是直接使用数组的indexOf方法来判断,如果元素存在于数组中,那么返回元素在数组中的下标值,如果不存在,那么返回-1,注意indexOf是区分大小写的,字母O必需大写,不然是会报错的,另外,该方法在某些版本的IE中是不起作用的,因此在使用之前需要做一下判断,修改后的代码如下所示: /** * 使用indexOf判断元素是否存在于数组中 * @param {Object} arr 数组 * @param {Object} value 元素值 */function isInArray3(arr,value){ if(arr.indexOf&&typeof(arr.indexOf)=='function'){ var index = arr.indexOf(value); if(index >= 0) {return true;}} return false;}
The third way is to use jquery's inArray method, which returns the subscript of the element in the array, or-1 if it does not exist in the array. The code is as follows:
/ * * use jquery's inArray method to determine whether the element exists in the array * @ param {Object} arr array * @ param {Object} value element value * / function isInArray2 (arr,value) {var index = $.inArray (value,arr); if (index > = 0) {return true;} return false;}
This method can be used to delete elements with unknown subscript values in an array, as shown in the following code:
Var arr = ['axiaojiaozhuo']; console.info ("Array before deleting elements:" + arr); arr.splice ($.inArray ('axiaojiarr), 1); console.info ("Array after deleting elements:" + arr)
The result of the execution is:
[Web browser] "Array before deleting elements: Array before deleting elements: test/index.html (12) [Web browser]" Array after deleting elements: test/index.html (14) above are all the contents of the article "how to determine whether the value is in the array by javascript". 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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.