Skip to content

Instantly share code, notes, and snippets.

@maabed
Forked from rauschma/api-cheatsheet-array.md
Created April 7, 2018 17:04
Show Gist options
  • Save maabed/46fb8049ee8b415490b81dc1a163d2bf to your computer and use it in GitHub Desktop.
Save maabed/46fb8049ee8b415490b81dc1a163d2bf to your computer and use it in GitHub Desktop.

Array<T>

Array<T>.prototype.*

  • concat(...items: (T | ConcatArray<T>)[]): T[]
    • Non-destructively concatenates this and the parameters (which can be single elements or array of elements).
    • ES3, non-destructive
    • ['a'].concat('b', ['c', 'd']) → [ 'a', 'b', 'c', 'd' ]
  • copyWithin(target: number, start: number, end?: number): this
    • Copies the elements whose indices range from start to (excl.) end to indices starting with target. Overlapping is handled correctly.
    • ES6, destructive
    • ['a', 'b', 'c', 'd'].copyWithin(0, 2, 4) → [ 'c', 'd', 'c', 'd' ]
  • entries(): Iterable<[number, T]>
    • Returns an iterable over [index, element] pairs.
    • ES6, non-destructive
    • Array.from(['a', 'b'].entries()) → [ [ 0, 'a' ], [ 1, 'b' ] ]
  • every(callback: (value: T, index: number, array: Array<T>) => boolean, thisArg?: any): boolean
    • Returns true if callback returns true for every element. Stops as soon as it receives false. Math: ∀
    • ES5, non-destructive
    • [1, 2, 3].every(x => x > 0) → true
    • [1, -2, 3].every(x => x > 0) → false
  • fill(value: T, start?: number, end?: number): this
    • Assigns value to every index.
    • ES6, destructive
    • [0, 1, 2].fill('a') → [ 'a', 'a', 'a' ]
  • filter(callback: (value: T, index: number, array: Array<T>) => any, thisArg?: any): T[]
    • Returns an array with only those elements for which callback returns true.
    • ES5, non-destructive
    • [1, -2, 3].filter(x => x > 0) → [ 1, 3 ]
  • find(predicate: (value: T, index: number, obj: T[]) => boolean, thisArg?: any): T | undefined
    • The result is the first element for which predicate returns true. If it never does, the result is undefined.
    • ES6, non-destructive
    • [1, -2, 3].find(x => x < 0) → -2
    • [1, 2, 3].find(x => x < 0) → undefined
  • findIndex(predicate: (value: T, index: number, obj: T[]) => boolean, thisArg?: any): number
    • The result is the index of the first element for which predicate returns true. If it never does, the result is -1
    • ES6, non-destructive
    • [1, -2, 3].findIndex(x => x < 0) → 1
    • [1, 2, 3].findIndex(x => x < 0) → -1
  • forEach
  • includes
  • indexOf
  • join
  • keys
  • lastIndexOf
  • map
  • pop
  • push
  • reduce
  • reduceRight
  • reverse
  • shift
  • slice
  • some
  • sort
  • splice
  • toLocaleString
  • toString
  • unshift
  • values

How holes are handled is described in Sect. “Array operations and holes” in “Exploring ES6”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment