Adding, deleting, modifying, checking, traversing and common methods of javascript array

  1. Increase::
    push(item1, item2...)Insert […, item1, item2… ] at the end
    unshift(item1, item2, ...): Insert [ item1, item2… , …] at the beginning
    splice(startIndex, ?deleteCount, item1, item2...): Any position
    concat(?item, ?Array): Insert at the end, return array copy
  2. Delete::
    pop()Delete the last element of the array, and return
    shift(): Delete the first element of the array, and return
    slice(startIndex, ?endIndex): Delete the specified area element
    splice(startIndex, ?deleteCount): Delete the specified number of elements at the beginning of the specified area, and return the deleted elements in the form of a list. When deleteCount is greater than the remaining elements, the elements before startIndex are retained.
  3. change
    splice(startIndex, ?deleteCount, item1, item2...)
  4. Check
    [index]: Out of bounds return undefine
    indexOf(item, ?startIndex): search from the beginning, return -1 if not found
    lastIndexOf(item, ?startIndex): search from the end, return -1 if not found
  5. Traversal
    for循环
    for(let item of array) { }: normal response continue, break, return, Item can not modify the original array
    forEach( (item, index) => { } ): can not respond continue, break, return, Item can not modify the original array
  6. Some commonly used methods
    map((value, index, array)=>{})
    filter((value, index, array)=>{})
    reduce((pre, curr, index, array)=>{}, initValue)
    reduceRight((pre, curr, index, array)=>{}, initValue)
    find((value, index, array)=>{ return 布尔值 })
    findIndex((value, index, array)=>{ return 布尔值 })
    keys(), values(), entries(): Return iterator object

Guess you like

Origin blog.csdn.net/SJ1551/article/details/109295038