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 | Array<T>)[]): T[] πŸ”’

    • Returns a new array that is the concatenation of this and all items. Non-array parameters are treated as if they were arrays with single elements.
    • ES3
    • ['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
    • ['a', 'b', 'c', 'd'].copyWithin(0, 2, 4) β†’ [ 'c', 'd', 'c', 'd' ]
  • entries(): Iterable<[number, T]> πŸ”’

    • Returns an iterable over [index, element] pairs.
    • ES6
    • 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
    • [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
    • [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
    • [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
    • [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
    • [1, -2, 3].findIndex(x => x < 0) β†’ 1
    • [1, 2, 3].findIndex(x => x < 0) β†’ -1
  • forEach(callback: (value: T, index: number, array: Array<T>) => void, thisArg?: any): void πŸ”’

    • Calls callback for each element.
    • ES5
    • ['a', 'b'].forEach((x, i) => console.log(x, i))
  • includes(searchElement: T, fromIndex?: number): boolean πŸ”’

    • Returns true if searchElement is an element and false, otherwise.
    • ES2016
    • [0, 1, 2].includes(1) -> true
    • [0, 1, 2].includes(5) -> false
  • 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