An analysis of JavaScript arrays

Source: Internet
Author: User

Create a new array

var num = new Array ();

Simple notation

var num = [n/a];

There are many ways to output an array

Alert (Num.join ("&")); Join ("|") Get an array that has | been split

Stack method

The stack method of the array push () adds a new value after the array, returns the new length, POPs () deletes the last value of the end of the array, returns the value that was deleted

var nums = Num.push ("4"); NUM returns [1,2,3,4]

var nums = Num.pop ()//nums==4 removes the last item of the array

Shift () moves the first numeric value in the array, returns the removed value, and unshift () adds a new value to the front end of the arrays, returning the new values

var shit = Color.shift ();

var shit = Color.unshift ();

These four methods can be combined with a variety of effects, (FIFO, LIFO), and generate a similar stack method

There are two ways to sort the array from the reverse () by flipping the arrays directly to sort () This method can be used to set the ordering conditions, can be from small to large can also be from large to small method see below T1 (from small to large) T2 (from largest to smallest);

//Reorder DemosfunctionT1 () {functionCOM (A, b) {if(a<b) {return-1;}Else if(a>b) {return1;}Else{return0;}}varValue = [0,1,5,10,15];value.sort (COM); alert (value)}t1 ();functionT2 () {functionCOM (A, b) {if(a<b) {return1;}Else if(a>b) {return-1;}Else{return0;}}varValue = [0,1,5,10,15];value.sort (COM); alert (value)}t2 ();

Copy operation of Array
Concat () method
var colors = ["Red", "green", "blue"];
Copies the current value and creates a new array, adds the received parameter to the end of the new array, and returns a new array;
var colors2 = Colors.concat ("Yellow", "black");
alert (colors2);//red,green,blue,yellow,black
The slice () method can receive two parameters, both returning the start and return position of the item;
var colors3 = Colors.slice (1,3);
Alert ("COLORS3:" +COLORS3);//Green,blue

Delete, insert, replace splice () method
Delete specified two parameters
var num = [1,2,3,4,5,6,7];
The first parameter represents the starting position of the operation, and the second parameter represents the deletion of several
var num1 = Num.splice (2,3);
alert (num);//1,2,5,6 is removed (3,4,5)
Inserts a parameter that specifies three parameters for the third argument that needs to be inserted
var num2 = Num.splice (2,0, "Red", "Black");
alert (num);//1,2,red,black,5,6
Replace can insert any number of items at the specified location and delete any number of items at the same time
var num3 = Num.splice (2,2, "red_s", "black_s"),//num's three or four-bit value is removed
alert (num);//1,2,red_s,black_s,5,6

Narrowing an array
Reduce receives four parameters, the previous value, the current value, the index of the item, and the array object
var reduces = [1,2,3,4,5,6];
var reduces_s = reduces.reduce (function (a,b,c,d) {
return a+b;
});
Alert ("Reduce:" +reduces_s);

There are many uses of arrays, so let's write so much today.

An analysis of JavaScript arrays

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.