Summary of common JavaScript Array Methods

Source: Internet
Author: User
JavaScript is a loose language, which may cause more trouble. For example

JavaScript is a loose language, which may cause more trouble. For example, the definition and use of JavaScript arrays are too flexible and sometimes confusing. Next we will list the common methods and definitions of arrays in JavaScript, hoping to help you better understand JavaScript arrays.

Five methods for declaring Arrays
Var a = new Array (); var a = new Array (10); // create an Array object, specify the number of entries in the Array var a = new Array ("red", "blue", "green"); var a = ["red", "blue ", "green"];
Three methods to define array attributes: constructor, length, prototype

Constructor indicates the function used to create an object. The constructor attribute is a member of all objects with prototype. They include all inherent JScript objects except Global and Math objects. The constructor attribute stores references to the functions used to construct a specific object instance. For example:

X = new String ("Hi"); if (x. constructor = String) // process (the condition is true ). Or function MyFunc {// function body .} Y = new MyFunc; if (y. constructor = MyFunc) // process (the condition is true ).

Length indicates the Length of the array. This will not be used as an example.

Prototype returns the reference of the Object Type prototype. The prototype attribute provides a set of basic functions of the object class. The new instance "inherit" of the object to authorize the operation of the object prototype. For example, you want to add a method to the Array object to return the maximum element value in the Array. To accomplish this, declare the function, add it to Array. prototype, and use it.

function array_max(){   var i, max = this[0];   for (i = 1; i < this.length; i++)   {   if(max < this[i])      max = this[i];   }   return max;}Array.prototype.max = array_max;var x = new Array(1, 2, 3, 4, 5, 6);var y = x.max();

After the code is executed, y saves the maximum value in array x, or 6.

Common 12 array Methods

Concat method | join method | pop method | push method | reverse Method | shift method | unshift method | slice Method | splice method | sort method | toString method | valueOf Method

// 1. concat (): returns a new array consisting of two or more arrays. Var a1 = [,]; var a2 = a1.concat ("5", '6'); alert (a2); // result, 5, 6 // 2. join (): returns the string value, which contains all elements of the connected array. The elements are separated by the specified separator. Var a1 = [1, 2, 4]; var a2 = a1.join (); var a3 = a1.join (","); var a4 = a1.join ("| "); alert (a2); // result: 1, 2, 3, 4, alert (a3); // result: 1, 2, 4, alert (a4); // result: 1 | 2 | 3 | 4 // 3.pop(): removes the last element from the array and returns this element. If this array is empty, undefined is returned. Var a1 = [1, 2, 4]; var item = a1.pop (); alert (item); // result: 4 alert (a1); // result: 1, 2, 3 // 4. push (): Add (append) the new element to an array and return the new Length Value of the array. The push method adds new elements in the order they appear. If one of the parameters is an array, the array will be added to the array as a single element. To merge two or more elements in an array, use the concat method. Var a1 = [,]; a1.push (5); a1.push ("6"); alert (a1); // result:, // 5. reverse (): returns an Array object whose element order is reversed. Var a1 = [,]; a1.reverse (); alert (a1); // result:, // 6. shift (): removes the first element from the array and returns the element. Var a1 = [1, 2, 4]; a1.shift (); alert (a1); // result: 2, 3, 4 // 7. unshift (): insert the specified element into the starting position of the array and return the array. Var a1 = [1, 2, 4]; a1.unshift (5); alert (a1); // result: 5, 1, 2, 3, 4 // 8. slice (): returns an array. A1.slice (start, [end]), the slice method is always copied to the specified end element, but this element is not included. If start is negative, it is processed as length + start. Here, length is the length of the array. If end is negative, it is processed as length + end. Here, length is the length of the array. If end is omitted, the slice method will always be copied to the end of arrayObj. If end appears before start, no elements are copied to the new array. Var a1 = [1, 2, 4]; var a2 = a1.slice (-1); var a3 = a1.slice (0,-1); var a4 = a1.slice (1 ); alert (a2); // result: 4 alert (a3); // result: 1, 2, 3 alert (a4); // result: 2, 4/9. splice (): removes one or more elements from an array. If necessary, insert a new element to the position of the removed element and return the removed element. // ArrayObj. splice (start, deleteCount, [item1 [, item2 [,... [, itemN]) var a1 = [,]; var a2 = a1.splice (); var a3 = a1.splice (); var a4 = a1.splice, "5"); alert (a2); // result: alert (a3); // result: 2 alert (a1); // result: 1, 5, 4 // 10. sort (): returns an Array object whose elements have been sorted. Var a1 = [, "B", "a"]; var a2 = a1.sort (); alert (a2); // result:,, B // 11. toString (): String Representation of the returned object. Var a1 = [, "B", "a"]; var a2 = a1.toString (); alert (a2); // result:, B, a // 12. valueOf (): returns the original value of the specified object. Var a1 = [, "B", "a"]; var a2 = a1.valueOf (); alert (a2); // result:, B,

IndexOf extension:

If (! Array. prototype. indexOf) Array. prototype. indexOf = function (item, I) {I | (I = 0); var length = this. length; if (I <0) I = length + I; for (; I <length; I ++) if (this [I] = item) return I; return-1 ;}; function testIndexOf () {var a = [1, 2, 4]; alert (. indexOf (3 ));} 

Address of this article: http://www.nowamagic.net/librarys/veda/detail/297,welcome.

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.