The in_array function implemented by JavaScript, javascriptin_array

Source: Internet
Author: User

The in_array function implemented by JavaScript, javascriptin_array

In JS, You need to determine whether a value is directly used by no function in the array. For example, the in_array () function exists in PHP. However, we can write a function similar to in_array () to determine whether a value is in the function.

/*** JS determines whether a value exists in the array * // defines a judgment function var in_array = function (arr) {// determine 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); // if (! IsArr) {throw "arguments is not Array";} // indicates whether the traversal is in the Array for (var I = 0, k = arr. length; I <k; I ++) {if (this = arr [I]) {return true ;}} // if it is not in the array, false return false will be returned;} // Add the prototype String to the String. prototype. in_array = in_array; // Add the Prototype Number to the numeric type. prototype. in_array = in_array; // declare an Array var arr = Array ('blue', 'red', '123', '123 '); // 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

If the input is not an array, an exception is thrown.

 

/*** JS determines whether a value exists in the array * // defines a judgment function var in_array = function (arr) {// determine 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); // if (! IsArr) {throw "arguments is not Array";} // indicates whether the traversal is in the Array for (var I = 0, k = arr. length; I <k; I ++) {if (this = arr [I]) {return true ;}} // if it is not in the array, false return false will be returned;} // Add the prototype String to the String. prototype. in_array = in_array; // Add the Prototype Number to the numeric type. prototype. in_array = in_array; // declare an array var arr = null; // string test var str = 'red'; var isInArray = str. in_array (arr); alert (isInArray); // uncaught exception: arguments is not Array

Direct error:


What is the function of adding two numbers in javascript?

Since priceArray [I]. value is a string, if you add it directly, it is processed by string connection. You should first convert it to a number:
TotalPrice = totalPrice + parseInt (priceArray [I]. value );
In fact, the standard javascript syntax is:
TotalPrice + = parseInt (priceArray [I]. value );

In javascript, how can we customize functions to implement the push function of Array objects?

Follow your procedures
This line

New_push (arr, "20"); // use a custom function to add new array elements

Should be changed to arr = new_push (arr, "20 ");

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.