Array objects (array objects) in JavaScript _javascript tips

Source: Internet
Author: User

1. Create an Array object method:

--->var arr = [1,2,3];//Simple definition method

Now you know

Arr[0] = = 1;
ARR[1] = = 2;
ARR[2] = = 3;
--->new Array ();
The var arr = new Array (),//defines an array object that has no content, and then assigns a value to it in the following manner
arr[0] = "Arr0";
ARR[1] = "arr1";
ARR[2] = "ARR2";
--->new array (size);//define an array object with a limited size, and then assign a value in the following way (assignment 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 contents of the array
var arr = new Array ("Arr0", "arr1", "arr2");

At this point you know:

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

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

--->length, that is, the length of the array
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:

arr.length=5;
document.write (Arr.length)//The output result is 5
--->prototype, which gives you the ability to add properties and methods to the object.
function MyArray (name,age)//defines a class that currently has two properties
{
this.name = name;
This.age = age;
}
var Myarr = new MyArray ("John");
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:

var arr = new Array (1,2,3);
Alert (Arr.concat (4,5));//Output 1,2,3,4,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---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.

---> Non-parametric
var arr = new Array ("Jone", "Grrgy", "John");
Alert (Arr.join ());//output Jone,grrgy,john the middle of the string, separated
---> a parameter
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)

var arr = new Array ("Jone", "John", "Grrgy");
document.write (Arr.pop ())//output: 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:

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:

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

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:

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

function
{
if (a>b)
{return
1;
}
Sortnumber (a,b)//Definition collation else if (a<b)
{
return-1
}
else
{return
0;
}

}
var arr = new Array (1,2000,3,400);
document.write (Arr.sort (Sortnumber));//Here only write the name of the function can be
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
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
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

---> Delete and add (equivalent to replace)
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 usually in the background of JavaScript automatic call, rarely used in the foreground

And this method cannot be implemented in IE browser, example: in Firefox

var Myarr = new Array (' Lisi ');
document.write (Myarr.tosource ());

The output results are:

["Lisi", 25]

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

function MyArray (name,age)
{
this.name = name;
This.age = age;
}
var Myarr = new MyArray (' Lisi ');
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 () can not, can only, separated, 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 ());

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

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

The above is a small set of JavaScript to introduce the array object (array object) of the relevant knowledge, hope to 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: 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.