JavaScript to determine whether a value in the array does not directly use the _ Basics

Source: Internet
Author: User
Tags numeric
In JS to determine whether a value in the array and no function directly use, such as PHP has In_array () this function. But we can write a similar in_array () function to determine whether a value is in a function

Example 1

Copy Code code as follows:


/*
*
* Determine if a given variable value is included in the array
Parameters
* Needle: The value to be queried
* Haystack: Array of queries
* In haystack query needle exists, if found returns True, otherwise return false.
* This function can only be valid for characters and numbers
*
*/

function FindNum () {
var a=[1,2];//assuming that a is an array, obj is the number to judge
var obj=1;
var B = false;
for (var i = 0; i < a.length; i++) {
if (a[i] = = obj) {
b = true;break;
}
}
if (b)
Alert ("An array exists a[" + i + "]:" + a[i]);
Else
Alert ("+obj" does not exist in the array);
}

Example 2
Copy Code code as follows:

/**
* JS to determine whether a value exists in the array
* Jones Taiwan Blog
*/

Define a judgment function
var in_array = function (arr) {
Determines whether the parameter is an array
var Isarr = arr && Console.log (
typeof arr=== ' object '? Arr.constructor===array? Arr.length? Arr.length===1? Arr[0]:arr.join (', '): ' An empty array ': arr.constructor:typeof arr
);

Throw an exception if it is not an array
if (!isarr) {
Throw "arguments is not Array";
}

Traversal is in the array
for (Var i=0,k=arr.length;i<k;i++) {
if (This==arr[i]) {
return true;
}
}

Returns False if it is not in the array
return false;
}

To add a prototype to a string
String.prototype.in_array = In_array;
To add a prototype to a numeric type
Number.prototype.in_array = In_array;

Declaring an array
var arr = Array (' Blue ', ' red ', ' 110 ', ' 120 ');

String test
var str = ' red ';
var Isinarray = Str.in_array (arr);
alert (Isinarray); True

Digital test
var num = 119;
var Isinarray = Num.in_array (arr);
alert (Isinarray); False
Throws an exception if the passed-in is not an array
/**
* JS to determine whether a value exists in the array
* Jones Taiwan Blog
*/

Define a judgment function
var in_array = function (arr) {
Determines whether the parameter is an array
var Isarr = arr && Console.log (
typeof arr=== ' object '? Arr.constructor===array? Arr.length? Arr.length===1? Arr[0]:arr.join (', '): ' An empty array ': arr.constructor:typeof arr
);

Throw an exception if it is not an array
if (!isarr) {
Throw "arguments is not Array";
}

Traversal is in the array
for (Var i=0,k=arr.length;i<k;i++) {
if (This==arr[i]) {
return true;
}
}

Returns False if it is not in the array
return false;
}

To add a prototype to a string
String.prototype.in_array = In_array;
To add a prototype to a numeric type
Number.prototype.in_array = In_array;

Declaring an array
var arr = null;

String test
var str = ' red ';
var Isinarray = Str.in_array (arr);
alert (Isinarray); Uncaught exception:arguments is not Array
JS determines whether there is a duplicate value in an array
var ary = new Array ("111", "22", "33", "111");
var s = ary.join (",") + ",";
for (Var i=0;i<ary.length;i++) {
if (S.replace (ary[i]+ ",", ""). IndexOf (ary[i]+ ",") >-1) {
Alert ("There are repeating elements in the array:" + ary[i]);
Break
}
}

Example 5
Copy Code code as follows:

function Isrepeat (arr) {
var hash = {};
for (var i in arr) {
if (Hash[arr[i]])
return true;
Hash[arr[i]] = true;
}
return false;
}

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.