Summary of JavaScript array methods

Source: Internet
Author: User
Tags javascript array

The company has no project recently. So fortunately learn JS. After all, many people are the same as me. The JS Foundation is also very poor.

The interviewer asks you when you are interviewing. You will JS not. You will not hesitate to answer the meeting. Because you did. But what really happens is probably just the method that was needed in the previous project.

In view of this situation I decided to put JS basic articles like Java or. NET in the blog park to explain one to.

Although the first hair technology paste. But I still read a lot of posts. I'm not going to be very irresponsible. Copy the code and leave. The following is a summary section:

<script type= "Text/javascript" >function Array () {var colors = ["Red", "Blue", "green"];//detect array if (colors instanceof ARR ay) {//If colors is an array. JS3 Method}if (Array.isarray (colors)) {//If colors is an array. JS5 method}//Conversion Method alert (colors.tolocalestring ()); Red,blue,greenalert (Colors.tostring ()); Red,blue,greenalert (Colors.valueof ()); Red,blue,greenalert (Colors.join (",")); The stack method of the red,blue,green//array//push () var count = Colors.push ("AAA", "BBB", "CCC"); Add a parameter alert (count) at the end of the original array; The added length is 6//pop () var enditem = Colors.pop (); Remove the last parameter alert (Enditem); Shows the removed parameter "CCC"//array of the queue method//shift () var firstitem = Colors.shift (); Remove the first parameter alert (FirstItem); Displays the removed "red"//unshift () var newcount = Colors.unshift ("000", "111", "222"); Add parameter alert (Newcount) at the top of the array; Display the added array length//reorder method var values = [0, 1, 5, 15];//recerse () alert (Values.reverse ()); Reverse order by number//sort () alert (Values.sort ()); Ascending but each comparison parameter is added ToString () so sort by string//But can be forced to convert under ArraySort method alert (Values.sort (Arraysort.reverse)); Reverse alert (Values.sort (Arraysort.sort)); Ascending//Operationmethod var letter = ["A", "B", "C"];//concat () var letter1 = Letter.concat (["D", "E"], ["F", "G"]);//Add one or more new arrays based on the current method alert (let Ter1);//a,b,c,d,e,f,g//slice () var letter2 = Letter1.slice (1, 4);//Remove the first and 4th parameters to form a new array alert (LETTER2);//b,c,e,d,g// Splice () parameter 1 to modify the start position of the deleted add parameter 2 The argument that follows the number to be deleted represents the item to be added alert (letter.splice (0, 1));//Deletes an item from [0] of the array alert (Letter.splice (2, 0 , "AA", "BB"), and/or start with an array [2] to add AA Bbalert (Letter.splice (2, 1, "CC", "DD"));//delete an item from array [2] and add cc dd//position method//indexof () alert (Letter.indexof (4));//Find//lastindexof () alert (Letter.lastindexof (4)) from the beginning of the array;//Search from the end of the array//iterative method//every () Letter = new Array (); letter = [1,2,3,4,5,6,7,8,9,10];var Everyletter = letter.every (function (item, index, array) {//every method with 3 parameters Each item in the item array. Index of each item of index. The array itself returns item > 2;//when each item in the array satisfies more than 2 o'clock. Returns true//This example return false} If 1 is less than 2 when looping to the first item);//some () var someletter = The Letter.some (item, index, array) {//some method has three parameters as the every method. Return to item > 2;//when any one of the arrays satisfies this condition, it returns true// In this example, when going to the third 3 is greater than 2 o'clock directly returns true});//filter () var filtErletter = Letter.filter (The function (item, index, array) {//filter method usage is the same as every and some.//But returns the result of each item included in the array return item > 2 ;//This example returns all numbers greater than 2. such as 3,4,5,6,7,8,9,10});//map () var mapletter = Letter.map (function (item, index, array) {// The map method usage is the same as every and some.//But the result of the return is the result of running the incoming function on the corresponding item in the original array; return item * 2;//This example I multiply each item by 2. The result is 2. 4,6,8,10,12,14,16,18,20});//foreach () Letter.foreach (the function (item, index, array) {//foreach method does not return a result.//And for loops Just around is the array//where you can do any operation});//Narrowing Method//reduce () var = [1, 2, 3, 4, 5];var sum = value.reduce (function (item1, ITEM2, Index, a Rray) {//reduce has a parameter item1 the previous value, item2 the current value, the index and array are the same as the every and some methods//the first time the item1=1,item2=2 is executed in this example, and the second time. Item1=3 (1+2) , item2=3, third execution 6 4, 4th execution 10,5//So the result is 15.return item1 + item2;//15});//reduceright () var sum1 = value.reduceright (function (Item1, item2, index, array) {The difference between//reduceright and reduce is that it is actually the reverse execution//So the result is 15return item1 + ITEM2;//15});} The extension of the sort () method is mainly for the conversion of the array var arraysort = {Reverse:function (VA1,VA2) {if (Va1 > Va2) {return 1;} else if (VA1 < VA2) {return-1;} else {return 0;}},sort:function (VA1, VA2) {if (Va1 > Va2) {return-1;} else if (VA1 & Lt VA2) {return 1;} else {return 0;}}; </script>

  

Summary of JavaScript array methods

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.