Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active April 11, 2023 21:31
Show Gist options
  • Save rauschma/f7b96b8b7274f2e2d8dab899803346c3 to your computer and use it in GitHub Desktop.
Save rauschma/f7b96b8b7274f2e2d8dab899803346c3 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(searchElement: T, fromIndex?: number): number 🔒
    • Return the index of the first element that is strictly equal to searchElement. Return -1 if there is no such element. Start searching at index fromIndex.
    • ES5
    • [0, 1, 2].indexOf(1) → 1
    • [0, 1, 2].indexOf(5) → -1
  • 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”.

@OliverMensahDev
Copy link

Please, can I join you in this quest? I will do my research well on these API's and start contributing.

@mrmartineau
Copy link

@donsalvadori
Copy link

how is this project/gist useful over MDN?

@rauschma
Copy link
Author

rauschma commented Apr 2, 2018

@omensah: Alas, it’s a solo project, but thanks for the offer!

@rauschma
Copy link
Author

rauschma commented Apr 2, 2018

@donsalvadori: it complements MDN. For details, you have to consult it.

@jthoms1
Copy link

jthoms1 commented Apr 2, 2018

I like the type information supplied by TypeScript. More detail on params. https://github.com/Microsoft/TypeScript/blob/7b9ceb85fa4e19ade740faa2af2e00e62e16f7c9/src/lib/es2015.core.d.ts#L37

But the examples are a must. 👍

@WebReflection
Copy link

WebReflection commented Apr 2, 2018

it'd be nice to specify which method involves Symbol.species or which method doesn't

edit actually, I guess that T[] would imply species, right? If that's the case, ignore my comment.

@fearphage
Copy link

I saw this the other day: Array Explorer. A cli tool to help you determine which array method you want/need to use. I'm not sure who it's designed for, but it's interesting nonetheless.

@t-cool
Copy link

t-cool commented Apr 3, 2018

Good article!! Could I translate this into Japanese?
I'll write down the link to this article.

@marcinnajder
Copy link

Nice summary! Yet another way of documenting using only examples https://github.com/marcinnajder/powerseq (tooltips)

Copy link

ghost commented Apr 6, 2018

I think you're missing smoosh 😆

@shoebsd31
Copy link

This is awesome

@elrumordelaluz
Copy link

Could be useful this interactive Array Explorer by @sdras

@rendertom
Copy link

There's a typo in push methods description: "Adds adds <...>"

@liuliangsir
Copy link

Here is a similar stuff.

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