JS reference type base--array

Source: Internet
Author: User

arrays, as the name implies, combine a set of numbers, that is, an array is an ordered collection of data.
The JS array is untyped, and the array element can be any type of data,
JS arrays are dynamic, and they grow or shrink as needed.
We know that an array is an object, and the index of an array is actually the property name of the object, except that it can only be accessed in square brackets.

  1. Create an array
    (1) Structural function method
    Use the array () constructor to create an array.
    var New Array ();          // Create a new array var New Array (ten);        // create an empty array of length 10, 10 is not the upper bound of the number of array elements

    (2) Literal notation
    Arrays are created using square brackets, and the array elements are separated by commas.

    var color = ["Blue", "red", "yellow"]; var count = [1,,3];                        // The array has 3 elements and the middle element is undefined var undefs = [,,,];                        // do not write like this, will be used as 3 or 4 elements, depending on the browser

    The elements of the above array count intermediate are slightly different if explicitly defined as undefined

    Although both arrays are 3 in length, the former is a sparse array with only 2 elements and a dense array with a number of 3 elements.

  2. Array length
    The array has a length property, in general, the length property value equals the number of array elements. The length value is greater than the number of array elements when the array is a sparse array
    The array length is dynamic, so you can delete the array element by changing the value of the length property

    Of course, since the array is an object, it inherits the method of the object, and we can make the length property of the array read-only by Object.defineproperty ().
    var a = [All-in-a--"length", {writable:false= 0; //The operation is not valid
  3. addition and deletion of array elements
    The simplest way to add an array element is to assign a value to the new index
    var a = [];         // start is an empty array a[0] = "zero";      // add element a[1] = "one";

    You can also use the push () method to add one or more elements to the end of the array

    var a = [];             // start creating an empty array a.push ("Zero");         // add an element at the end, a=["Zero"], and the method returns 1a.push ("One", "two");    // Add two additional elements, a = ["zero", "one", "two"], and the method returns 3

    The length of the new array returned by the method.
    To insert an element in the header of an array, you can use the Unshift () method, which also returns the length of the new array.
    You can delete an array element with the length property above, or you can delete an array element using the delete operator as you would delete an object property

    var a = [n/a]; delete a[1];        // a no longer has an element in the position of index 1  in a              //falsea.length            //3

    As you can see, delete does not modify the length property of the array
    You can also use the Pop () method and the Shift () method of an array to delete an array element. The Pop () method deletes the last element at the end of the array at a time, the method returns the deleted element, and the shift () method deletes the element at the header of the array, and the same method itself returns the deleted element. Unlike delete, the index position of the delete other element is the same, that is, the sparse array is formed, and the corresponding element index of the pop () and Shift () method arrays is automatically reduced by 1, which is a dense array.
    Finally, splice () is a generic way to insert, delete, and replace an array element, which changes the original array and returns the removed element. The new array is still a dense array

  4. ES3 Array Method
    4.1 Join ()
    Converts all elements in an array to strings concatenated together, passing in a parameter as a delimiter, separated by commas if no parameters are available. The method does not change the original array
    The Array.join () method is the reverse operation of the String.Split () method
    4.2 Reverse ()
    Reverses the order of the elements in the array, changes the original array, and returns the new array.
    4.3 Sort ()
    Sorts the elements in the array. Changes the original array and returns the new array. If the array contains undefined elements, they will be queued at the end of the array
    Sort () is sorted alphabetically, and if you want to sort by numeric size, you can pass in a comparison function
    4.4 concat ()
    Creates and returns a new array that does not change the original array.
    var a = [];a.concat (4,5);              // return [1,2,3,4,5]a.concat ([4,5]);            // return [1,2,3,4,5]a.concat ([4,5],[6,7]);      // return to [1,2,3,4,5,6,7]A.concat (4,[5.[ 6,7]]);      // return [1,2,3,4,5,[6,7]]

    4.5 Slice ()
    Returns a fragment or sub-array of the specified array, with two parameters starting and ending positions, respectively, but not containing the end position. Does not change the original array
    4.6 Splice ()
    The first parameter of the method specifies the starting position, the second parameter is the number of deletions from the array, and if omitted, it is deleted from the beginning to the end, and the other arguments are the elements to be inserted. Changes the original array, returns an array of deleted elements, and returns an empty array if no elements are deleted.
    4.7 push () and pop ()
    4.8 unshift () and Shift ()
    4.9 toString () and toLocaleString ()

  5. ES5 Array method
    5.1 ForEach ()
    Iterates through the array, invokes the specified function for each array, passes the function as the first argument, and then foreach () calls the function using 3 parameters: The array element, the index of the element, and the group itself
    5.2 Map ()
    Passes each element of the called array to the specified function and returns an array that contains the return value of the array.
    5.3 Filter ()
    Returns a subset of the array elements that the division calls an array. The passed function is used to determine that the function returns TRUE or FALSE.
    5.4 Every () and some ()
    The Every () and some () methods are logical judgments of arrays, which use the specified function for array elements to determine, returning true or false
    5.5 reduce () and reduceright ()
    5.6 IndexOf () and LastIndexOf ()
    Searches for an element in the entire array that has the given value, returns the index of the first element found, or 1 if no element is returned.
  6. Array type
    We can use the TypeOf operator to easily identify basic type data, but not to distinguish between arrays and non-array objects, because they all return "objects", and in ES5 we can use the array.instanceof operator to do this work,


2017-10-09
19:21:23

JS reference type base--array

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.