Common methods of arrays

The basic structure

  • Object data type arrays also have property names, but the property names are indexes;
  • Index represents the position of the item in the array, starting at index 0
  • Length represents the length of the array

An array of class

  • The collection of elements obtained by getElementsByTagName is an array of classes
  • The argument collection in the function is also an array of classes

Class arrays cannot use array methods

Iterate over each term of the set

Var ary =,6,9,3 [1]; for (var i = 0; i<ary.length; i++) { console.log(ary[i]); }Copy the code

Common way to change the original array

Use console.dir(array.prototype) to view all methods

Remember methods in these areas

  1. The meaning and function of the method
  2. Method parameter
  3. Method return value
  4. Whether the original array is changed by this method

increase

Push: appends to the end

  • Parameters: Any data type, one to more, separated by commas
  • Return value: The length of the new array
  • The original array has changed

Unshift: Appends to the beginning

  • Parameters: Any data type, one to more, separated by commas
  • Return value: The length of the new array
  • The original array has changed

Treat an array as a normal object, using key-value pair operations

  • ary[ary.length]=xxx; // Append to end

delete

Pop: Deletes the last item in the array

  • Parameters: no
  • Return value: The deleted item
  • The original values have changed

Shift: Deletes the first item in the array

  • Parameters: no
  • Return value: The deleted item
  • The original values have changed

Delete the first entry and each subsequent index change

Treat the array as a normal object and delete it using key-value pairs

  • Delete Delete: delete ary[index] deletes the specified item, with other indexes unchanged and length unchanged
  • ary.length–; Deletes the last item in the array

Splice can add, delete, or change

delete

  • Splice (n,m) : deletes m entries starting from index n
  • Return value: Deleted content (saved as a new array)
    • Splice (0) empties the array
    • Splice () does not delete a single entry
    • Splice (ary.length-1) Deletes the last item

Modify the

  • Splice (n,m,x) : replaces the deleted content with x

increase

  • Splice (n,0,x) : insert x in front of index N without deleting any entries
    • Ary.splice (0,0,x) appends at the beginning
    • Ary.splice (ary.length,0,x) appends to the end

Method that does not change the original array

ForEach: Iterates through each item in the array

  • Parameter: function, two parameters (item,index) item is each item,index is the index
  • Return value: undefined
Var ary = [1, 2, 3]; ary.forEach( function(item,index) {console.log(item+"----"+index) }) ary//==>undefinedCopy the code

Array clone: Slice

  • Parameter: slice (n,m) copy index m from index n (excluding M)
  • Return value: Returns the found portion as a new array
    • Slice (n) copies the end from index n
    • Slice () or Slice (0) array clone, same as before

  • Negative numbers are supported in terms of total length + negative index

Concatenate two arrays: concat

  • Parameter: The content to concatenate (placed after the array), which can be of any data type
  • Return value: new array after concatenation
    • Concat () has nothing to concatenate, equivalent to cloning
    • If you concatenate an array, you will unconcatenate the array inside
    • One more layer of parentheses will concatenate it as an array

indexOf/lastIndexOf

IndexOf (lastIndexOf) : gets the indexOf the first (last) occurrence of the current item in the array, and returns -1 if there is no such item, to verify that the array contains this item

If (arry.indexof (12)>-1){arry.prototype =function myIndexOf(value){var reselt=-1; for(var i=0; i<this.length; i++){ if(value===this[i]){ result=i; break; } } return result; }Copy the code

includes

  • One argument: the object to find
  • Return value: true if; No return false
[1, 2, 3].includes(2)     // true
[1, 2, 3].includes(4)     // false
[1, 2, NaN].includes(NaN) // true
Copy the code
  • The second argument to this method represents the starting position of the search, which defaults to 0. If the second argument is negative, it represents the reciprocal position, and if it is greater than the array length (for example, if the second argument is -4, but the array length is 3), it is reset to start at 0
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
Copy the code

Sorting and permutation in arrays

Reverse: Arranges each item in an array upside down

  • Parameters: no
  • Return value: sorted order

Sort: implements array sort

  • Parameters: none or callback function
  • Return value: sorted array (same address as the original array)
    • In the absence of arguments: Sort by the first digit only

A way to arrange two digits

ary.sort(function(a,b){ return a-b; // return b-a; / / / / descending}), for example, var ary1 = [{age: 14}, {age: 21}, {age: 1}, {age: 13}, {age: 11}] ary1. Sort (function (a, b) {return a.a ge - b.a ge; // In ascending order by age})Copy the code

Convert an array to a string

toString

  • Parameters: no
  • Return value: Converted to a string separated by commas

join

  • Argument: “(empty string)
  • Return value: a string without commas
  • Parameters: no
  • Return value: a string with a comma
  • Parameters: the ‘+’
  • Return value: string delimited by +

If you want something as a delimiter, you pass the delimiter into an empty string, okay

Eval (not often used)

  • Parameter: a string type, which is run as js code