InArray in JS checks whether a value exists in the array

Source: Internet
Author: User

Development often use this same logic, to determine whether a string or number in the array, many programming languages have such a special function, such as often used in PHP has a in_array (), but the JS incredibly wood there, wood you know?

Baidu also has a lot of ways to achieve, here to leave a kind of their own commonly used!

Attached code:

function In_array (Stringtosearch, Arraytosearch) {for

(s = 0; s < arraytosearch.length; s++) {

thi        SEntry = Arraytosearch[s].tostring ();

if (thisentry = = Stringtosearch) {return

true;

return

false;

}

Usage:

var abc=[1,2,3,4];

var z = 4;

if (In_array (Z,ABC)) {

Console.log (' Congratulations, including ' +z ');

} else{

console.log (' Sorry, not including ' +z ');

}

This article by the shadow shallow ' s Blog in July 2015, published in the front-end development category, you can comment, and retain the original address and the author of the case to refer to your website or blog

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.