JavaScript Array Object Detail _ basics

Source: Internet
Author: User
Tags javascript array

1. Create an Array object method:

--->var arr = [Element0, Element1, ..., elementn];//simple definition method

Copy Code code as follows:
var arr = [1,2,3]

Now you know
Copy Code code as follows:
Arr[0] = = 1;
ARR[1] = = 2;
ARR[2] = = 3;

--->new Array ();

Copy Code code as follows:
var arr = new Array ();//defines an array object that has no content, and assigns values to it in the following way
Arr[0] = "ARR0";
ARR[1] = "arr1";
ARR[2] = "ARR2";

--->new array (size);//define an array object of limited size, and then assign a value in the following way (the assignment is the same as above)

Copy Code code as follows:
var arr = new Array (3);
Arr[0] = "ARR0";
ARR[1] = "arr1";
ARR[2] = "ARR2";

--->new array (element0, element1, ..., elementn);//directly define the contents of the array

Copy Code code as follows:
var arr = new Array ("Arr0", "arr1", "arr2");

At this point you know:
Copy Code code as follows:
Arr[0] = = "Arr0";
ARR[1] = = "ARR1";
ARR[2] = = "ARR2";

2, Array object properties

There are three common properties for array: constructor, length, and prototype

--->constructor, as the name suggests, is a constructor, that is, what constitutes this object, and then the popular point is the type of object, see the following example

Copy Code code as follows:
var arr = new Array (3);
if (Arr.constructor==array)
{
document.write ("This is a 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 result of the above output is that this is a Array

--->length, that is, the length of the array

Copy Code code as follows:
var arr = new Array (3);
document.write (arr.length)//output result is 3

Note that in JavaScript, you can modify the properties of an array object.

So:

Copy Code code as follows:
arr.length=5;
document.write (arr.length)//output result is 5

--->prototype gives you the ability to add properties and methods to an object.

Copy Code code as follows:
function MyArray (name,age)//defines a class that currently has two properties
{
THIS.name = name;
This.age = age;
}
var Myarr = new MyArray ("John", 25);
Myarray.prototype.test = null;//Adds a property to the MyArray class
Myarr.test = "Test";
alert (myarr.test);//Output test

3. Concat () method---> Connect two or more arrays

It has two ways of using it:

---> Connect actual data
Cases:

Copy Code code as follows:
var arr = new Array (1,2,3);
Alert (Arr.concat (4,5));//Output 1,2,3,4,5

---> Connect two or more arrays

Copy Code code as follows:
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---the elements in the array into a string

It can have parameters or no parameters, and the arguments represent a way to split the generated string.

---> No ginseng

Copy Code code as follows:
var arr = new Array ("Jone", "Grrgy", "John");
Alert (Arr.join ());//Output Jone,grrgy,john string in the middle, separating

---> Ginseng

Copy Code code as follows:
var arr = new Array ("Jone", "Grrgy", "John");
Alert (Arr.join (".")); /Output Jone. Grrgy.john strings separated by arguments

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

Copy Code code as follows:
var arr = new Array ("Jone", "John", "Grrgy");
document.write (Arr.pop ());//output content: Grrgy
document.write (Arr.join ("-"));//output: Jone-john

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

If the argument in push () is empty (not filled in), the original length of the array is returned and no changes are made to the arrays
Cases:

Copy Code code as follows:
var arr = ["Jone", "John", "Grrgy"];
document.write (Arr.push ("Tom"))//output: 4 (length)
document.write (Arr.join ());//output: Jone,john,grrgy,tom

7, reverse () reverse the order of elements in the array, no parameters

Cases:

Copy Code code as follows:
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)

Copy Code code as follows:
var arr = ["Jone", "John", "Grrgy"];
document.write (Arr.shift ());//output: Jone
document.write (Arr.join ());//output: Jone,john

9, Slice () returns the specified element from the specified array, note: it returns an array

It has two parameters, start and end,
Start is required, specifying the position of the starting element
End is optional, the location of the specified ending element, if not written, to the end of the array
Cases:

Copy Code code as follows:
var arr = ["Jone", "John", "Grrgy", "Tom", "Hell"];
var test = Arr.slice (1);
if (Test.constructor==array)
{
document.write ("This is a array<br>");
document.write (Test.join ());
}

Final result output:

This is a Array
John,grrgy,tom,hell

If the var test = Arr.slice (1) is changed to:
var test = Arr.slice (1,2);
The result output is:
John

10, sort () The elements of the array are sorted, a very important method

It can have parameters, the parameter is a function (), this function sets the rules for sorting,
Note, it produces a copy of the original array, does not generate a new array, that is, based on the original array to modify
If you do not add parameters, you will follow the sort methods built into JavaScript, in alphabetical order
Cases:

Copy Code code as follows:
var arr = ["Jone", "John", "Grrgy", "Tom", "Hell"];
document.write (Arr.sort ());
document.write ("<br>");
document.write (arr);

The output results are:
Grrgy,hell,john,jone,tom
Grrgy,hell,john,jone,tom

The following are sorted by number size

Copy Code code as follows:
function Sortnumber (A,B)//functions that define collations
{
if (a>b)
{
return 1;
}
else if (a<b)
{
return-1;
}
Else
{
return 0;
}

}
var arr = new Array (1,2000,3,400);
document.write (Arr.sort (Sortnumber));//Here only the function name can be written
document.write ("<br>");
document.write (arr);

Output:

1,3,400,2000
1,3,400,2000

11, splice () Delete elements and add elements to the array

Splice (INDEX,HOWMANY,ELEMENT1,ELEMENT2.....ELEMENTX) describes the following:
Index is required to specify where to add/remove elements. This parameter is the subscript for the array element that starts inserting and/or deleting, and must be a number.
Howmany is required. Specify how many elements should be deleted. Must be a number, but it can be "0". If this parameter is not specified, all elements that start from index to the end of the original array are deleted.
When Howmany is 0, it means that no elements are deleted and that the implication is to add only
Element1 is optional, which sets the new element to be added to the array. Inserts from the subscript at index point, you can insert multiple
The difference between splice () and slice () is that splice () is a processing of the original array, which modifies the value of the original array and returns an array.
Splice () is the equivalent of replacing an element in an array, or inserting or deleting

Look at the following three examples:

---> Insert only

Copy Code code as follows:
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 results:
George,john,thomas,james,adrew,martin
George,john,william,thomas,james,adrew,martin
William was inserted in the 2 position.

---> Delete only

Copy Code code as follows:
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 results:
George,john,thomas,james,adrew,martin
George,john,james,adrew,martin
Deleted the elements of the original array 2 position

---> Both delete and add (equivalent to substitution)

Copy Code code as follows:
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 results:
George,john,thomas,james,adrew,martin
George,john,william,james,adrew,martin
To replace Thomas with William.

12, Tosource () return the source code of the object, this method is generally located in the background of JavaScript automatic call, rarely used in the foreground and this method can not be implemented in IE browser, example: in Firefox

Copy Code code as follows:
var Myarr = new Array (' Lisi ', 25);
document.write (Myarr.tosource ());

The output results are:
["Lisi", 25]

If you redefine a class, you can display the property name, for example:

Copy Code code as follows:
function MyArray (name,age)
{
THIS.name = name;
This.age = age;
}
var Myarr = new MyArray (' Lisi ', 25);
document.write (Myarr.tosource ());

The output results are:
({name: "Lisi", age:25})
A bit like the JSON type of data, but it's just similar, it's not a JSON data type format

13, ToString (), the array is returned as a string, and it is the same as the result of the join (), but the join () method can customize the symbol for the interval and toString () is not, can only be, separated, for example:

Copy Code code as follows:
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 ());

The output results are:
Jone.john.Tom
Jone,john,tom
Jone,john,tom
Jone,john,tom
You can see the results of the following 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, and the original array will change

Copy Code code as follows:
Unshift (Element1,element2,element3 ...), at least one element, example:
var Myarr = new Array (' Jone ', ' John ', ' Tom ');
var length = myarr.unshift (' Zhangsan ', ' Lisi ');
document.write (Myarr);
document.write (' <br> ');
document.write (length);

The output results are:
Zhangsan,lisi,jone,john,tom
5

See more JavaScript syntax, you can focus on: JavaScript reference tutorial, JavaScript Code style guide, and also hope that many people support the cloud-Habitat community.

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.