Common methods of javascript-arrays

Source: Internet
Author: User

I do not know if you have a similar experience with me, that is, when transduction began to do the cut chart page, often call some others write jquery plug-ins, such as music player this need to switch multiple music plug-ins. Call must have a music queue, and this queue is an array, and the demand side want to be in the HTML of the page through the date property to get to JavaScript can automatically generate a list of songs. At that time I did not systematically read some of the basics of JavaScript, his husband two monks, don't touch the head, do not know how to do.

And then I checked the ECMAScript. This object has natively encapsulated many useful methods, so I would like to summarize some commonly used array built-in methods here:

1. Join

var colors = ["Red", "Blue", "Green"];colors.join ("$$");   Colors.tostring ();   // "red$ $blue $ $green"

Join () is used to change the segmentation of an array.

2. How to modify the internal data of an array

//*//* Insert Method//*//Push Methodvarcolors = ["Red"];colors.push ("Blue", "green");//3Colors.tostring ();//"Red,blue,green"//Unshift Methodvarcolors = ["Red"];colors.unshift ("Blue", "green");//3Colors.tostring ();//"Blue,green,red"//*//* Delete (exit) array Item method//* //Pop Methodvarcolors = ["Red", "Blue", "green"];colors.pop (); //"Green"//Shift Methodvarcolors = ["Red", "Blue", "green"];colors.shift (); //"Red"//*//* The most powerful method--splice (index position, number of items to be deleted, number of data to be inserted)//*//for deletion purposesvarcolors = ["Red", "Blue", "green"];colors.splice ();//["Blue"]Colors.tostring ()//"Red,green"//for replacement purposesvarcolors = ["Red", "Blue", "green"];colors.splice ("White", "Black");//["Blue", "green"]Colors.tostring ();//"Red,white,black"//use as Insertvarcolors = ["Red", "Blue", "green"];colors.splice (1,0, "white", "Black");//[]Colors.tostring ();//"Red,white,black,blue,green"

The push () method is to insert an array item at the end of the array, and Unshift () is to insert the array item from the top of the array, noting that the inserted array item remains in the order of the array with the parameter.

The Pop () method is the last "popup" array item from the array, while Unshift () is the first "popup" array item from the array.

The splice method can be used to insert, delete, and replace these 3 functions:Splice (index position, number of items to be deleted, number of data to be inserted)

3. Iterative method

varnumbers = [1,2,3,4,5,4,3,2,1];//*//* Determine the array for each item//*//every only each entry returns true, the whole will return trueNumbers.every (function(Item,index,array) {returnITEM>2})//false//some as long as one item returns TRUE, the whole will return trueNumbers.some (function(Item,index,array) {returnITEM>2})//true//*//*filter filter that returns an array of conforming items//*Numbers.filter (function(Item,index,array) {returnITEM>2})//[3, 4, 5, 4, 3]//*//* An iterative iteration that is executed individually//*//Map has a return resultNumbers.map (function(Item,index,array) {returnITEM*2})//[2, 4, 6, 8, ten, 8, 6, 4, 2]//foreach does not return resultsNumbers.foreach (function(Item,index,array) {Array[index] = item*2}); alert (numbers); //[2, 4, 6, 8, ten, 8, 6, 4, 2]

We can invoke the original iterative method provided by JS according to different requirements!!

4. Merge method

Finally, the merge method, reduce () and reduceright (), both iterate over all the items of the algebraic group, where reduce () starts with the first item, and Reduceright () starts with the last item in the array, except that two functions are identical

//reduce () The first time an array first, the second start for the first function return value is the prevarvalues = [1,2,3,4,5];varSum=values.reduce (function(Pre,cur,index,array) {returnPre +cur}); alert (sum); // the//Reduceright () The pre is the first array last, the second starts with the first function return value as prevarvalues = [1,2,3,4,5];varSum=values.reduceright (function(Pre,cur,index,array) {returnPre +cur}); alert (sum); // the

In fact, foreach () can also be fully implemented, and more than the above two merge methods to understand better, how to achieve this function also see personal habits.

Common methods of javascript-arrays

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.