Skip to content

Instantly share code, notes, and snippets.

@fjmiguel
Created May 24, 2022 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjmiguel/0c0791beefafc2c45fe802037fdd4b30 to your computer and use it in GitHub Desktop.
Save fjmiguel/0c0791beefafc2c45fe802037fdd4b30 to your computer and use it in GitHub Desktop.
JS - Which array method to use

JS - Which array method to use?

Array built-in object documentation

To mutate original array

Add to original

  • .push() (end)
  • .unshift() (start)

Remove from original

  • .pop() (end)
  • .shift() (start)
  • .splice() (any)

Others

  • .reverse()
  • .sort()
  • .fill

A new array

Computed from original

  • .map() (loop)

Filtered using condition

  • .filter()

Portion of original

  • .slice()

Adding original to other

  • .concat()

Flattening the original

  • .flat()
  • .flatMap()

An array index

Based on value

  • .indexOf()

Based on test condition

  • .findIndex()

An array element

Based on test condition

  • .find()

Know if array includes

Based on value

  • .includes()

Based on test condition

  • .some()
  • .every()

A new string

Based on separator string

  • .join()

To transform to value

Based on accumulator

  • .reduce()

To just loop array

Based on callback

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