In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to judge whether the javascript element is in the array, Xiaobian feels quite practical, so share it with you for a reference, I hope you can gain something after reading this article.
Methods: 1. Use indexOf() function to get the first occurrence position of the specified element in the array. If the return value is "-1", the element is not in the array;2. Use lastIndexOf() to get the last occurrence position of the specified element in the array. If the return value is "-1", the element is not in the array.
Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.
In javascript, indexOf() and lastIndexOf() functions can be used to determine whether an element is in an array.
The indexOf() and lastIndexOf() methods retrieve array elements, returning the index position of the specified element or "-1" if the specified element does not exist.
Find elements in an array using indexOf()
indexOf() returns the index of the first occurrence of an element value in an array, or-1 if the specified value is not found. The usage is as follows:
array.indexOf(item,start)
item must. Find the element.
start Optional integer argument. Specifies where in the array to start retrieving. Its legal values are 0 to stringObject.length - 1. If this parameter is omitted, the search starts with the first character of the string.
The indexOf() method performs a search by ascending index, i.e., retrieving from left to right. Retrieving makes congruent comparisons between array elements and searchElement parameter values ===.
Example: Find whether an element is in an array
var arr = ["ab","cd","ef","ab","cd"];var str="cd";if(arr.indexOf(str)===-1){ console.log("specified element: "+str+"not in array");}else{ console.log("specify element: "+str+"in array");}
Change the value you want to find:
var str="gh";if(arr.indexOf(str)===-1){ console.log("specified element: "+str+"not in array");}else{ console.log("specify element: "+str+"in array");}
Find elements in an array using lastIndexOf()
indexOf() returns the index of the last match of an element value in an array, or-1 if the specified value is not found. The usage is the same as indexOf().
Example: Find whether an element is in an array
var arr = ["ab","cd","ef","ab","cd"];var str="gx";if(arr.lastIndexOf(str)===-1){ console.log("specified element: "+str+"not in array");}else{ console.log("specify element: "+str+"in array");}
About "javascript how to determine whether elements in the array" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
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.