Array object (Array object) and javascriptarray in JavaScript

Source: Internet
Author: User

Array object (Array object) and javascriptarray in JavaScript

1. method for creating an Array object:

---> Var arr = [1, 2, 3]; // simple definition method

Now you can know

Arr [0] = 1; arr [1] = 2; arr [2] = 3; ---> new Array (); var arr = new Array (); // define an array object without any content, and assign arr [0] = "arr0"; arr [1] = "arr1" to it in the following way "; arr [2] = "arr2"; ---> new Array (size); // defines an Array object with limited size, then assign values in the following way (the assignment method is the same as above) var arr = new Array (3); arr [0] = "arr0 "; arr [1] = "arr1"; arr [2] = "arr2"; ---> new Array (element0, element1 ,..., elementn); // directly define the Array content var arr = new Array ("arr0", "arr1", "arr2 ");

Now you can know:

arr[0] == "arr0";arr[1] == "arr1";arr[2] == "arr2";

2. Array Object Attributes

Array has three common attributes: constructor, length, and prototype.

---> Constructor, as its name implies, is a constructor, that is, what the object is composed of. In other words, it is the object type. See the following example.

var arr = new Array(3);if(arr.constructor==Array){document.write("This is an Array");}if (test.constructor==Boolean){document.write("This is a Boolean");}if (test.constructor==Date){document.write("This is a Date");}if (test.constructor==String){document.write("This is a String");}

The output above is: This is an Array

---> Length, that is, the length of Array var arr = new Array (3); document. write (arr. length); // The output result is 3.

Note: In Javascript, you can modify the attributes of an Array object,


Arr. length = 5; document. write (arr. length); // The output result is 5 ---> prototype, which enables you to add attributes and methods to an object. Function myarray (name, age) // defines a class. this class currently has two attributes: {this. name = name; this. age = age;} var myarr = new myarray ("john", 25); myarray. prototype. test = null; // adds an attribute myarr to the myarray class. test = "test"; alert (myarr. test); // output test

3. concat () method ---> concatenate two or more Arrays

It can be used in two ways:

---> Connect to the actual data


Var arr = new Array (, 3); alert (arr. concat (); // output, 5

---> Connect two or more Arrays

Var arr1 = new Array (1, 2, 3); var arr2 = [4, 5]; var arr3 = new Array ("jone", "john"); alert (arr1.concat (arr2, arr3); // output 1, 2, 3, 4, 5, jone, john

4. join () method ---> put the elements in the array into a string

It can have parameters or no parameters. The parameters represent the method of separating the generated string.

---> Var arr = new Array ("jone", "Grrgy", "john"); alert (arr. join (); // output the jone, Grrgy, and john strings separated by commas (,) ---> var arr = new Array ("jone", "Grrgy", "john "); alert (arr. join (". "); // output jone. grrgy. the john string is separated by parameters.

5. the pop () method is used to delete and return the last element of the array (before deletion)

Var arr = new Array ("jone", "john", "grrgy"); document. write (arr. pop (); // output content: grrgydocument. write (arr. join ("-"); // output: jone-john

6. The push () method is used to add an element to the last array and return the length of the array (after adding)

If the parameter in push () is null (not filled), the original length of the array is returned without any modification to the array.

Var arr = ["jone", "john", "grrgy"]; document. write (arr. push ("tom"); // output: 4 (length) document. write (arr. join (); // output: jone, john, grrgy, tom

7. reverse the order of elements in the array with no parameters


var arr = ["jone","john","grrgy"];document.write(arr.reverse());//grrgy,john,jone

8. shift () Delete and return the first element of the array (before deletion)

Var arr = ["jone", "john", "grrgy"]; document. write (arr. shift (); // output: jonedocument. write (arr. join (); // output: jone, john

9. slice () returns the specified element from the specified array. Note: It returns an array.

Its Parameters include start and end,
Start is required. It specifies the position of the start Element.
End is optional. It specifies the position of the end element. If it is not written, it is considered to end with the array.


var arr = ["jone","john","grrgy","tom","hell"];var test = arr.slice(1);if(test.constructor==Array){document.write("This is an Array<br>");document.write(test.join());}

Final result output:

This is an Array
John, grrgy, tom, hell

If you change var test = arr. slice (1):

var test = arr.slice(1,2);

Result output:


10. sort () is an important method for sorting array elements.

It can have parameters. The parameter is a function (), which specifies the sorting rules,
Note: It generates a copy of the original array and does not generate a new array, that is, it is modified on the basis of the original array.
If no parameter is added, it is sorted alphabetically by the built-in sorting method in Javascript.


var arr = ["jone","john","grrgy","tom","hell"];document.write(arr.sort());document.write("<br>");document.write(arr);

Output result:

Grrgy, hell, john, jone, tom
Grrgy, hell, john, jone, tom

Sort by number

Function sortNumber (a, B) // function defining the sorting rule {if (a> B) {return 1;} else if (a <B) {return-1 ;} else {return 0 ;}}var arr = new Array (3,400,); document. write (arr. sort (sortNumber); // write only the function name here to document. write ("<br>"); document. write (arr );


11. splice () deletes an element and adds it to the array.

Splice (index, howmany, element1, element2.... elementx) is described as follows:

Index is required to specify where to add/delete elements. This parameter is the subscript of the array element that begins to insert or delete. It must be a number.
Howmany is required. Specifies how many elements should be deleted. It must be a number, but it can be "0 ". If this parameter is not specified, all elements starting from index to the end of the original array are deleted.
When howmany is 0, it indicates that no elements are deleted. The implication is to add only
Element1 is optional and specifies the new elements to be added to the array. Insert from the subscript indicated by index. you can insert multiple
The difference between splice () and slice () Is that splice () processes the original array. It modifies the value of the original array and returns an array.
Splice () is equivalent to replacing or inserting or deleting an element in the array.

Let's look at the following three examples:

---> Insert only var arr = new Array (6); arr [0] = "George"; arr [1] = "John"; arr [2] = "Thomas "; arr [3] = "James"; arr [4] = "Adrew"; arr [5] = "Martin"; document. write (arr + "<br/>"); arr. splice (2, 0, "William"); document. write (arr + "<br/> ");

Output result:

George, John, Thomas, James, Adrew, Martin
George, John, William, Thomas, James, Adrew, Martin

William inserted to 2

---> Delete only var arr = new Array (6); arr [0] = "George"; arr [1] = "John"; arr [2] = "Thomas "; arr [3] = "James"; arr [4] = "Adrew"; arr [5] = "Martin"; document. write (arr + "<br/>"); arr. splice (2, 1); document. write (arr + "<br/> ");

Output result:

George, John, Thomas, James, Adrew, Martin
George, John, James, Adrew, Martin

The element at the position of array 2 is deleted.

---> Both deleting and adding (equivalent to replacing) var arr = new Array (6); arr [0] = "George"; arr [1] = "John "; arr [2] = "Thomas"; arr [3] = "James"; arr [4] = "Adrew"; arr [5] = "Martin"; document. write (arr + "<br/>"); arr. splice (2, 1, "William"); document. write (arr + "<br/> ");

Output result:

George, John, Thomas, James, Adrew, Martin
George, John, William, James, Adrew, Martin

Replace the original Thomas with William.

12. toSource () returns the source code of the object. This method is generally automatically called in the background of Javascript and rarely used in the foreground.

This method cannot be implemented in IE browser, for example, in firefox

var myarr = new Array('lisi',25);document.write(myarr.toSource());

Output result:

["Lisi", 25]

If a new class is defined, the attribute name can be displayed, for example:

function myarray(name,age){ = name;this.age = age;}var myarr = new myarray('lisi',25);document.write(myarr.toSource());

Output result:

({Name: "lisi", age: 25 })

It is similar to Json data, but it is only similar. It is not a Json Data Type format.

13. toString (). The array is returned as a string. It returns the same result as the join () method. However, the join () method can customize the delimiter.

ToString () is not allowed. It can only be separated by commas (,). For example:

var myarr = new Array('jone','john','Tom');document.write(myarr.join('.'));document.write('<br>');document.write(myarr.join(','));document.write('<br>');document.write(myarr.join());document.write('<br>');document.write(myarr.toString());

Output result:

Jone. john. Tom
Jone, john, Tom
Jone, john, Tom
Jone, john, Tom

The results of the last three methods are the same.

14. unshift (). You can add one or more elements to the beginning of the array and return the new length of the array. The original array will change.

Unshift (element1, element2, element3....) has at least one element, for example:

var myarr = new Array('jone','john','Tom');var length = myarr.unshift('zhangsan','lisi');document.write(myarr);document.write('<br>');document.write(length);

Output result:

Zhangsan, lisi, jone, john, Tom

The above section describes the knowledge of Array objects (Array objects) in JavaScript. I hope it will help you!

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: 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.