JS Advanced Programming--array

Source: Internet
Author: User

var colors = new Array (20); Create an array with a length value of 20

var colors = new Array (' Red ', ' blue ', ' green '); Create an array with three strings;

You can also omit the new operation when using the array constructor, var colors = Array (3);

Array.isarray () method to determine if a value is an array

Some ways to do this:

Concat (); You can create a new array based on all the items in the current array

var colors = [' red ', ' green ', ' Blue '];
var colors2 = colors.concat (' Yellow ', [' black ', ' purple ']);
alert (colors)//Red,green,orange
Alert (COLORS2)//red,green,blue,yellow,black,purple

Slice (); Can accept one or two parameters, that is, the starting position and the end position, return all items starting at the end, but not the items that are ending, and in the case of only one argument, return all items from the specified position of the parameter to the end of the current array

Splice (); Change and delete

Three parameters, starting position, length, insert item;

Iterative methods:

Filter (); Use the specified function to determine whether an item is included in the returned array

var num= [1,2,3,4,5,4,3,2,1];

var result = Num.filter (function (Item,index,array) {

Return (item > 2);

})

alert (result);

Map ();

Also returns an array, similar to the one above

ForEach ();

var num= [1,2,3,4,5,4,3,2,1];

Num.foreach (function (Item,index,array) {

Perform certain actions

})

reduce (); and Reduceright (); Two methods for merging arrays

Loops through all the entries in the array and constructs a final return value that accepts 4 parameters: previous value, current value, index of item, and array object

var num= [1,2,3,4,5];

var result = Num.reduce (function (Prev,cur,index,array) {

return prev + cur;

})

alert (result); 15

JS Advanced Programming--array

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.