JavaScript, those methods to change the original array

Those methods alter the original array

  1. fill()

    grammar

    array.fill(value, start, end)

    parameter

    parameter description
    value essential. Fill.
    start Optional. Start filling position.
    end Optional. Stop filling position (by default Array .length)
  2. pop()

    grammar

    array.pop()

    This method changes the length of the array

  3. push()

    grammar

    array.push()

    Add one or more elements to the end of the array, and returns the new length.

    This method changes the length of the array

  4. reverse()

    grammar

    array.reverse()

    Change the original array

  5. shift()

    grammar

    array.shift()

    This method changes the length of the array

  6. sort()

    Definition and Usage

    sort () method is used to sort the elements of the array.

    Sort order may be letters or numbers, in ascending or descending order.

    The default sort order is ascending alphabetical order.

    Note: When the digital in alphabetical order "40" will be in the front row "5."

    Using digital ordering, you must be called by a function as a parameter.

    It is a function of the specified number in ascending or descending order.

    Note: This method changes the original array! .

    Parameter Values

    parameter description
    compareFunction Optional. The provisions of the sort order. It must be a function.
    • If no callback function is safe in ascending alphabetical order

    • If you provide a callback function then returns the value according to the callback function to sort, sort () will be an array of every element except the last element calls the callback function,

      The callback function requires two parameters, the first parameter represents the current element, the second argument represents an element after the current element (and therefore does not call for the last element)

    • a and b are two elements to be compared:
    • If compareFunction(a, b)less than 0, then a is arranged before B;

    • If compareFunction(a, b)equal to 0, the relative position of a and b unchanged. Note: ECMAScript standard does not guarantee this behavior, but not all browsers will follow (eg Mozilla versions prior to 2003);

    • If compareFunction(a, b)greater than 0, b is arranged before a.
    • compareFunction(a, b) It must always return the same result of the comparison to the same input, otherwise the sort of result will be uncertain.

  7. splice()

    grammar

    array.splice(index,howmany,item1,.....,itemX)

    Parameter Values

    parameter description
    index essential. Where the provisions of add / remove elements. This parameter is inserted starting and (or) an array element subscript deleted, it must be numeric.
    howmany Optional. It specifies how many elements should be removed. It must be a number, but can be "0." If this parameter is not specified, then remove all elements from index to the end of the original array.
    item1, ..., itemX Optional. To add a new element to the array

    return value

    Type description
    Array It returns an array containing the elements to be deleted.
  8. unshift()

    Add one or more elements to the beginning of an array and returns the new length.

    grammar

    array.unshift(item1,item2, ..., itemX)

    Parameter Values

    parameter description
    item1,item2, ..., itemX Optional. Adding one or more elements of the array to the initial position.

Guess you like

Origin www.cnblogs.com/wangbanshan/p/11444402.html