Every day a JavaScript learning summary (array of array) _javascript tips

Source: Internet
Author: User
Tags prev

1. Common methods of array

var colors = ["Red", "Blue", "green"];  Creates an array with three strings
    alert (colors.tostring ());  Red,blue,green
    alert (colors.valueof ());   Red,blue,green
    alert (colors);        Red,blue,green

2. Array map () method

var numbers = [1,2,3,4,5,4,3,2,1];
    
    var mapresult = Numbers.map (function (item, index, array) {
      //item array element index element corresponds to indexed array original array
      Console.log (array = = = numbers);//true return
      item * 2;
    });
    Console.log (Mapresult);  [2,4,6,8,10,8,6,4,2]

3. Array reduce () method

 var values = [1,2,3,4,5];
     Receives a function and then traverses the item from left to right until reduce to a value.
    var sum = values.reduce (function (prev, cur, index, array) {
      Console.log (array = = values);
      Console.log (index);//1,2,3,4 array is indexed starting from 1 return
      prev + cur;//before and after two values added
    });
    alert (sum);//15

4. Array concat () method

The Concat () method is used to connect two or more arrays.
    //The method does not change an existing array, but merely returns a copy of the connected array.
    //Grammar
    //arrayobject.concat (Arrayx,arrayx,......, arrayx)
    var colors = ["Red", "green", "Blue"];
    var colors2 = Colors.concat ("Yellow", ["Black", "Brown"]);
    
    alert (colors);   Red,green,blue    
    alert (colors2);  Red,green,blue,yellow,black,brown

5, length of the array

 var colors = new Array (3);   Create an array with three items
    var names = new Array ("Greg");//create a array with one item, the string "Greg" 
   alert (colors.length);//3
    alert (names.length);//1

 var colors = ["Red", "Blue", "green"]; Creates an array with three strings
    var names = [];            Creates an empty array
    var values = [1,2,];          avoid! Creates an array with 2 or 3 items
    var options = [,,,,,];         avoid! Creates an array with 5 or 6 items
    
    alert (colors.length);  3
    alert (names.length);   0
    alert (values.length);  2 (FF, Safari, Opera) or 3 (IE)
    alert (options.length);  5 (FF, Safari, Opera) or 6 (IE)

var colors = ["Red", "Blue", "green"];  Creates an array with three strings
    colors.length = 2;
    Alert (colors[2]);    Undefined
var colors = ["Red", "Blue", "green"];  Creates an array with three strings
    colors.length = 4;
    Alert (colors[3]);    Undefined

 var colors = ["Red", "Blue", "green"];  Creates an array with three strings
    colors[colors.length] = "BLACK";     Add a color
    colors[colors.length] = "Brown";     Add another color

    alert (colors.length);  5
    alert (colors[3]);    Black
    alert (colors[4]);    Brown

var colors = ["Red", "Blue", "green"];  Creates an array with three strings
    colors[99] = "BLACK";           Add a color (position)
    alert (colors.length);//100

6. Array methods every and some

The Every () and some () methods are iterative methods for arrays in JS.
    //every () is the given function that runs each item in the array, and returns True if the function returns true for each item.
    //some () is the specified function that runs each item in the array, and returns True if the function returns true for either item.
    var numbers = [1,2,3,4,5,4,3,2,1];
    
    var everyresult = numbers.every (function (item, index, array) {return
      (item > 2);
    });
    
    alert (everyresult);    False
    
    var someresult = numbers.some (function (item, index, array) {return
      (item > 2);
    });
    
    alert (someresult);    True

7. Array filter () method

Find the element that fits the condition from the array (for example, greater than the value of an element)
    var numbers = [1,2,3,4,5,4,3,2,1];
    
    var filterresult = numbers.filter (function (item, index, array) {return
      (item > 2);
    });
    
    alert (filterresult);  [3,4,5,4,3]

8, array indexof and LastIndexOf

The
 

9. Array toLocaleString and ToString

 var Person1 = {
      tolocalestring:function () {return
        ' Nikolaos ';
      },
      
      tostring:function () {return
        "N Icholas ";
      }
    };
    
    var Person2 = {
      tolocalestring:function () {return
        "Grigorios";
      },
      
      tostring:function () {
        re Turn "Greg";
      }
    ;
    
    var people = [Person1, Person2];
    alert (people);           Nicholas,greg
    alert (people.tostring ());      Nicholas,greg
    alert (people.tolocalestring ());   Nikolaos,grigorios

10. Array Push and Pop methods

var colors = new Array ();           Create an array
    var count = Colors.push ("Red", "green");    Push two items
    alert (count);//2
    
    count = Colors.push ("Black");         Push another item on
    alert (count);//3
    
    var item = Colors.pop ();            Get the last item
    alert (item);  "Black"
    alert (colors.length);//2

11, array method unshift and shift

The Unshift () method adds one or more elements to the beginning of the array and returns a new length. The
    //shift () method deletes the first element of an array and returns the value of the first element.
    var colors = new Array ();           Create an array
    var count = Colors.unshift ("Red", "green");  Push two items
    alert (count);//2
    
    count = Colors.unshift ("Black");        Push another item on
    alert (count);//3
    
    var item = Colors.pop ();           Get the
    item  alert "Green"
    alert (colors.length);//2

12. Array Reverse Method Reverse

 var values = [1, 2, 3, 4, 5];
    Values.reverse ();
    alert (values);    5,4,3,2,1

13, array sorting method sort

function Compare (value1, value2) {
      if (value1 < value2) {
        return-1;
      } else if (value1 > value2) {
   
    return 1;
      } else {return
        0;
      }
    }
    
    var values = [0, 1, a, m];
    Values.sort (compare);
    alert (values);  0,1,10,15,16
    //sort Change the original array

   

14, Array method slice

The
      /* Slice () method returns the selected element from an existing array.
      Syntax
      arrayobject.slice (start,end)
      start  required. Specify where to start the selection. If it is a negative number, then it sets the position from the end of the array. In other words,-1 refers to the last element, 2 to the penultimate element, and so on. End is    Optional. Specify where to end the selection. The parameter is the array subscript at the end of the array fragment. If this argument is not specified, the Shard array contains all the elements from start to the end of the array. If this argument is a negative number, it sets the element that starts at the end of the array. The
      return value
      returns a new array containing the elements in the arrayobject from start to end (excluding the element).
    * *
    var colors = ["Red", "green", "blue", "yellow", "purple"];
    var colors2 = Colors.slice (1);
    var colors3 = Colors.slice (1,4);
    
    alert (colors2);  Green,blue,yellow,purple
    alert (COLORS3);  Green,blue,yellow

15, Array method Splice

/* The
      Plice () method adds/deletes items to/from an array, and then returns the items that are deleted.
      Note: This method changes the original array.
      Syntax
      arrayobject.splice (index,howmany,item1,....., itemx)
      index  required. Integer that stipulates where to add/remove items, and use negative numbers to specify the position from the end of the array.
      Howmany  required. The number of items to delete. If set to 0, the item is not deleted.
      item1, ..., itemx optional  . The new item to add to the array.
    * *
    var colors = ["Red", "green", "Blue"];
    var removed = Colors.splice (0,1);       Remove the
    colors   Green,blue
    alert (removed);  Red-one Item array
    
    removed = Colors.splice (1, 0, "yellow", "orange");//insert two items at position 1
    alert (colors);   Green,yellow,orange,blue
    alert (removed);  Empty array

    removed = Colors.splice (1, 1, "Red", "purple");  Insert two values, remove one
    alert (colors);   Green,red,purple,orange,blue
    alert (removed);  Yellow-one Item Array

16. Array IsArray () method

Alert (Array.isarray ([]));  True
    alert (Array.isarray ({}));  False

The above is today's JavaScript learning Summary, and will continue to update every day, I hope you continue to pay attention.

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.