JavaScript version of the In_array function (to determine whether there are specific values in the array) _javascript tips

Source: Internet
Author: User

We often use this same logic to determine whether a string or number is in an array, and many programming languages have such a special function, such as PHP's In_array (). Then JS has wood there, unfortunately, JS Wood has such a function, so think of the great JQ whether encapsulated this function, found the API,JQ does encapsulate this function
Jquery.inarray (value, array) searches the array for the specified value and returns its index (1 if it is not found).
Value to search for.
An array of arrays through which to search.

Of course, in learning, I also write such a function:

Copy Code code as follows:

function InArray1 (needle,array,bool) {
if (typeof needle== "string" | | typeof needle== "number") {
for (var i in array) {
if (Needle===array[i]) {
if (bool) {
return i;
}
return true;
}
}
return false;
}
}

Three parameters, find Needle,bool in array as Boolean, and return needle position in array if true

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.