javascript--Array

Source: Internet
Author: User

1. Array type
ECMAScript arrays and arrays in other languages are sequential tables, but have the following characteristics:
A. Each item can hold any type of data.
B. The size of the array can be dynamically adjusted.
C. Length property of the array: readable writable, you can remove an item from the end of the array by setting the value of length or add a new item to the array

    1) Create method
       1. Use the array constructor
           var arr = new Array ();
          var arr = new Array (; )//pre-specified array size
           var arr = new Array ("Terry", "Larry", "Boss"); Incoming parameters
          Note: The New keyword can also be omitted, but try not to omit
        2. Use the array literal
          is represented by a pair of square brackets that contain array items, with multiple array items separated by commas
           var arr = ["Terry", "Larry", "Boss"];
          var arr = []   //empty array

2) Accessing array elements
Array variable name [index]
1. Returns the value of the corresponding item if the index is less than the length of the array
var arr = ["Terry", "Larry", "Boss"];
ARR[0]; Accesses the first element in the array, the return value is Terry
2. If the index is greater than the length of the array, the array automatically increases to the length of the index value plus 1
var arr = ["Terry", "Larry", "Boss"];
ARR[3] = "Jacky"; add element, array degree becomes 4

The array can contain a maximum of 4 294 967 295 Items

3) Check Array
var arr = [];
TypeOf (arr); Return object
Array.isarray (arr); Determine if arr is an array type


    4) Converts an array to a string
   array that inherits the object method, and overrides these methods
         tolocalstring ();
        toString ();   returns array items by default as a comma-delimited string
     Example:
    var arr = ["Terry", "Larry", "Boss"]; 
    arr.tostring () &nbs    P; //terry,larry,boss
    valueOf (), which is displayed by default in the form of an array literal (Firefox)
    Arr.valueof ();   ]//["Terry", "Larry", "Boss"]; 
   join ();  use the specified string to separate the array string
    Example:
    arr.join ("| |");   //briup| | terry| | Jacky

/*arr-array-{can inherit the method}-object*/
var arr = ["Terry", "Tom", 1,true,{age:12}];
Console.log (arr);
Call the ToString method inherited from the parent object
Console.log (Arr.tostring ());
Call the Join method inherited from the parent object
Console.log (Arr.join ("-"));
Serialization of
var json = json.stringify (arr);
Console.log (JSON);

5) Stack, queue method
1. Stack LIFO (last-in-first-out)
Push () Accepts arguments of any type, adds them to the end of the array one by one, and returns the length of the array
Pop () removes the last item from the end of the array, reduces the length value of the array, and returns the removed item
2. Queue FIFO (first-in-first-out)
Shift () shifts the first item in the array and returns the item, reducing the length of the arrays by one.
Unshift () adds any item to the front of the array and returns the length of the new array.

    6) Sort
       reverse ()   Reverse the order of array items
        sort ()   
         1. Default sort: The method invokes the ToString () transformation method of each array item and then sorts
         2. Custom sort:
     A. The method can accept a comparison function as a parameter, and the comparison function has two parameters
    B. Returns a negative number
    c if the first parameter precedes the second argument. Returns a positive number if the first argument is after the second argument
    var arr = [11,5,23,7,4,1,9,1];
    Console.log (Arr.sort (Compare));

This comparison function is suitable for most data types
function Compare (V1,V2) {
if (V1>V2) {
return-1;
}else if (v1<v2) {
return 1;
}else{
return 0;
}
}

   

var students = [{
Name: "Terry",
Age:12,
salary:3400,
Tostring:function () {
Return "3"
}
},{
Name: "Tom",
Age:13,
salary:1400,
Tostring:function () {
Return "1"
}
},{
Name: "Vicky",
Age:10,
salary:8400,
Tostring:function () {
Return "2"
}
}];

Console.log (students);

var result = Students.sort (function (key) {
Returns the Comparer function
return function (A, b) {
if (A[key] < B[key]) {
return-1;
}else if (A[key] > B[key]) {
return 1;
}
}
}) ("Age"));

Console.log (students);
Console.log (result);


7) Operation method
Concat (): Creates a copy of the current array and then adds the received parameter to the end of the copy, returning the copy
var arr = ["AA", "BB", "CC", "DD"];
var arr_new = arr.concat ("ee", "FF");
Arr_new = ["AA", "BB", "CC", "dd", "EE", "FF"]//arr does not change
Slice (): can accept one or two parameters (returns the starting position of the item, ending position)
When a parameter is accepted, starting at the position specified by the parameter, all items to the end of the current array
When you accept two parameters, the entry from the beginning to the end, but not the item that contains the end position
For example:
var arr = ["AA", "BB", "CC", "DD"];
1. When a parameter is accepted
var arr_new = Arr.slice (1);
arr_new = ["BB", "CC", "DD"],arr does not change
2. When two parameters are accepted
var = arr.slice (arr_new);
arr_new = ["BB"]; ARR does not change
Splice (): Inserting data into the middle of an array will always return an array containing items removed from the original array.
Delete: Specify two parameters (the starting position of the deletion, the number of items to delete)
Insert: Specify three parameters (starting position, 0, any number of items to insert)
Replace: Specify three parameters (starting position, item to be deleted, any number of items to insert)

For example:
var arr = ["AA", "BB", "CC", "DD"];
1. Delete
var = arr.splice (Del_arr);
arr = ["AA", "DD"]; Delete operation in original array
Del_arr = ["BB", "CC"]; Returns an array of deleted elements
2. Insert
var Del_arr = Arr.splice (1,0, "ee", "FF");
arr = ["AA", "ee", "ff", "BB", "CC", "DD"] Insert the specified item into the 1-bit place
Del_arr = [], returns an empty array
3. Replace
var Del_arr = arr.splice ("ee", "FF");
arr = ["AA", "ee", "FF", "DD"] Replace "BB", "CC" with "ee", "FF"
Del_arr = ["BB", "CC"], returns an array of deleted elements
IndexOf () (the item to find, the location to start finding (optional)) is searched backwards from the beginning of the array, using the strict equality operator, cannot find the element return-1
var arr = ["All", "one", "CC", "DD", "11"];
Arr.indexof (11); Returns-1 because the match is made using "= = ="
Arr.indexof ("11"); Returns 1, matches from the previous, returns the position of the first matching element
LastIndexOf () (the item to find, the location to start finding (optional)) looks forward from the end of the array, using the strict equality operator, cannot find the element return-1
var arr = ["All", "one", "CC", "DD", "11"];
Arr.lastindexof ("11"); Returns 4, followed by a forward match, returning the position of the first matching element

8) Iterative Method:
Parameters: A function running on each item, the scope object that runs the function (optional)
Every (); Returns true for each run of the given function in the array, if the function returns true for each item
var arr = [11,5,23,7,4,1,9,1];
var result = ARR.E
Every (function (Item,index,arr) {
Return Item >2;
});
Console.log (result); False
Some (); Returns true for each run of the given function in the array, if the function returns true for any of the entries
var result = Arr.every (function (Item,index,arr) {
Return Item >2;
});
Console.log (result); True
Filter (); An array of the items that satisfy the function is returned for each run of the given function in the logarithm group
var result = Arr.filter (function (Item,index,arr) {
Return Item >2;
});
Console.log (result); [11, 5, 23, 7, 4, 9]
Map (); An array of each run of the given function that returns the result of each function call
var result = Arr.map (function (Item,index,arr) {
return item * 2;
});
Console.log (result); [22, 10, 46, 14, 8, 2, 18, 2]
ForEach (); Every run of a given function in an array, no return value, commonly used to traverse an element
var result = Arr.foreach (function (Item,index,arr) {
Console.log (item);
});

javascript--Array

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.