Re-turn JavaScript after a year

Source: Internet
Author: User

think about your work. iOS development for more than a year the blink of an eye is over, but it's not over yet, and it's just that the previous development, though not at the apex, is a successful transformation through my tireless efforts, albeit at first through the JQ direct entry But in retrospect, as if they did not learn much more, today, today, I have again opened the JavaScript book, browse the inside of the various methods, feel that they want to learn or a lot of many, then today I will start from the array to explain my harvest today, brought to the array, I believe that the development of the familiar can not be familiar with, but talk about the inside of various methods, I believe like my rookie plus small white must be both familiar and unfamiliar, then follow my step into the array of the world bar, to learn the profound array.

  1. Array creation I won't say much here, and here's the first point of my study. How to detect whether an object is an array:

Of course, the method of detecting arrays is not only one, I will show you two native methods, the code:

/ ** * @description Detection object is not an array (this method has drawbacks, deprecated, this can occur in multiple global execution environments, with caution) * @param {Detection Object} * @return {[no return value]}*/            functionCheckarray (value) {if(ValueinstanceofArray) {Alert (1111); }            }            varCheck = [A.];            Checkarray (check); /** * @return {no return value}*/            functionCheckArray2 (value) {if(Array.isarray (value)) {Alert (' Recommended method '); }} checkArray2 (check);

The above code is the two methods of detecting arrays.

  2. The stack method of the array (so-called stack method that is, the array can behave like a stack, the latter refers to the ability to restrict the insertion and deletion of items, the stack is a LIFO (LIFO) data structure, that is, the last addition of the earliest removed, and the insertion of the item in the stack is called push, remove called eject, The push and pop methods are methods for inserting elements and removing elements for an array):

Push (): can accept any number of parameters and return the length of the modified array;

Pop (): Removes the element from the end of the array, reduces the length of the array, and returns the removed element;

Examples are attached below for your reference:

            var colors = [0,1,5,10,15];            alert (colors);             var returnnew = Colors.push (' A ', ' B ', ' C ');            alert (returnnew);             var delnew = colors.pop ();            alert (delnew);            

The above is the stack method of the array!

At the same time, the opposite of pop () is shift (), which removes the item from the front of the array and returns it, and can use the shift () and push () methods to simulate the queue operation (FIFO);

  3. Reordering of arrays:

When it comes to sorting arrays many people will definitely think of the sort () and reverse () methods first, but everyone knows that the sort () method simply sorts the strings, not so rigorous and reverse () method is the inverse of the array, and does not implement sorting, So to be very good in the perfect sort we can pass the sort function implementation in the sort () method, as in the following example:

/** * @description [comparison function] * @param {compare size} * @param {compare size} * @return {ratio More results}*/            functionCompare (value1,value2) {if(value1>value2) {                    return-1; } Else if(value1==value2) {                    return0; } Else {                    return1; }            }            varAry1 = [' Yellow ', ' red ', ' blue ']; varAry2 = [' 1 ', ' 2 ', ' 3 ', ' 4 ']; varArybig = Ary1.concat (Ary2, ' black ');//Merging Arrays            varSliceary=arybig.slice (2,4);//splitting an array does not affect the original array, except that the copy returns all elements from the start position to the end position.document.write (' <br> ' + ' array 1: ' +ary1+ ' <br> ' + ' array 2: ' +ary2+ ' <br> ' + ' Large Array: ' +arybig+ ' split array: ' +sliceary+ ' <br> ');

  The use of the 4.splice () method, its methods can delete elements, but also to the array to specify the location of the element, this method is one of the most powerful method, the following is a few columns to let everyone see, incidentally, this method is not like the above two methods (Concat and slice) Splice is worth inserting on the basis of modifying the original array, and the method returns the value removed from the array:

// /*            //use of array splice            //  */            varSpliary = [1,2,3,4,5,6,7,8]; varA = Spliary.splice (1, 3);//Delete 3 elements from the table below 1 to return [2,3,4]            varb = Spliary.splice (1, 0, ' A ', ' B ', ' C ');//add three elements; return an empty array            varc = Spliary.splice (1, 3, ' A ', ' C ', ' e ');//Delete and add return [2,3,4]document.write (spliary+ ' <br> ' +a+ ' <br> ' +b+ ' <br> ' +c+ ' <br> ');

  5. Array location methods (IndexOf and LastIndexOf)

Both methods can accept two parameters, the first parameter is required (find items), the second parameter is optional, that is, the starting position of the lookup, the only difference is the search order, the former is from the array to find, the latter is from the end of the array to find the location of the specified element. Returns 1 if not found, returns the position of the element in the array if found

  6. Iterative Methods for arrays

1.every () Each item of an array runs the specified function, and returns TRUE if each item of the function returns true;

2.filter () Each item of an array runs the specified function, and returns a set of true elements that are composed of arrays;

3.forEach () Each item of an array runs a given function, and the method does not return a value;

4.map () Each item of the array runs the given function, which returns an array consisting of each function call result;

5.some () Each item of an array runs the given function, and returns True if any one of the entries satisfies the condition to return true;

None of the above methods will modify the values in the original array. The following examples illustrate:

varArray = [1,4,6,2,7,3]; varEveryitem = Array.every (function(Item,index,array) {returnItem>2; }) document.write (Everyitem);//Output False            varEveryitem = Array.some (function(Item,index,array) {returnItem>2; }) document.write (Everyitem);//Output True            varEveryitem = Array.map (function(Item,index,array) {returnItem*2; }) document.write (Everyitem);//output [2,8,12,4,14,6]            varEveryitem = Array.filter (function(Item,index,array) {returnItem>2; }) document.write (Everyitem);//output [4,6,7,3]

7. The narrowing of the array is reduced () and reduceright (), the only difference is that one starts from the array and the other is the end of the array, both of which can accept four parameters, the first one is the previous item of the array, the second is the current item, the index value, and the group, So the difference between the two methods is that it depends on the convenience array from the beginning, with the simple code example below:

 var  array = [1,4,6,2,7,3 var  result = Array.reduce (function   (Prev,cur,index,array) { return prev+cur; }) document.write (result);  //             output 23 is 1+4+6+2+7+3  var  result = Array.reduceright (function   (Prev,cur,index,array) { return  PR            Ev+cur; }) document.write (result);  //  output 23 is 3+7+2+6+4+1  

The output is 23, but the order of addition is different, so this is the only difference between the two, the above is my understanding of the array today, but also a master of the new just, here I all record down, hope to let more friends see, can help more people, good today's learning and accumulation here, Tomorrow there will be time to continue, I hope that we have a lot of attention, there is nothing left to point out or give advice, bye!

Re-turn JavaScript after a year

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.