Summary of basic knowledge about javascript Array objects

Source: Internet
Author: User

My summary of the Array object is: Method 5, method 3, Method 12

1. Method 5 of declaration: only for one-dimensional arrays, of course, there are two-dimensional 3D. I will not explain it here.
Copy codeThe Code is as follows:
Var a = new Array ();
Var a = new Array;
Var a = new Array (10); // create an Array object and specify the number of items in the Array
Var a = new Array ("red", "blue", "green ");
Var a = ["red", "blue", "green"];

2. Attribute 3 strokes: 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:
Copy codeThe Code is as follows:
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 of the object "inherits" the operation that is granted to 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.
Copy codeThe Code is as follows:
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.
3. Method 12: Concat method | join method | pop method | push method | reverse Method | shift method | unshift method | slice Method | splice method | sort method | toString method | valueOf Method
Copy codeThe Code is as follows:
// 1. concat (): returns a new array consisting of two or more arrays.
Var a1 = [1, 2, 4];
Var a2 = a1.concat ("5", '6 ');
Alert (a2); // result: 1, 2, 3, 4, 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, 3, 4
Alert (a4); // result: 1 | 2 | 3 | 4
// 3.pop(): removes the last element from the array and returns the 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 = [1, 2, 4];
A1.push (5 );
A1.push ("6 ");
Alert (a1); // result: 1, 2, 3, 4, 5, 6
// 5. reverse (): returns an Array object whose element order is reversed.
Var a1 = [1, 2, 4];
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 segment. 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, 3, 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 = [1, 2, 4];
Var a2 = a1.splice (1, 0 );
Var a3 = a1.splice (1, 1 );
Var a4 = a1.splice (1, 1, "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 = [2, 3, 1, 4, "B", "a"];
Var a2 = a1.sort ();
Alert (a2); // result: 1, 2, 3, 4, a, B
// 11. toString (): String Representation of the returned object.
Var a1 = [1, 2, 3, 4, "B", "a"];
Var a2 = a1.toString ();
Alert (a2); // result: 1, 2, 3, 4, B,
// 12. valueOf (): returns the original value of the specified object.
Var a1 = [1, 2, 3, 4, "B", "a"];
Var a2 = a1.valueOf ();
Alert (a2); // result: 1, 2, 3, 4, B,

IndexOf extension:
Copy codeThe Code is as follows:
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 (a. indexOf (3 ));
}
<Input type = "button" value = "test to obtain the array subscript" onclick = "testIndexOf ()"/>

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.