JavaScript Array Tricks

Arrays are one of the most common data types in JavaScript. While there are many libraries that deal with them, here are some awesome tips and tricks you can leverage in your code without adding any external dependencies.

  • uniqueElements

    JavaScript, Array

    Finds all unique values in an array.

  • compact

    JavaScript, Array

    Removes falsy values from an array.

  • any

    JavaScript, Array

    Checks if the provided predicate function returns true for at least one element in a collection.

  • all

    JavaScript, Array

    Checks if the provided predicate function returns true for all elements in a collection.

  • none

    JavaScript, Array

    Checks if the provided predicate function returns false for all elements in a collection.

  • difference

    JavaScript, Array

    Calculates the difference between two arrays, without filtering duplicate values.

  • union

    JavaScript, Array

    Returns every element that exists in any of the two arrays at least once.

  • intersection

    JavaScript, Array

    Returns the elements that exist in both arrays, filtering duplicate values.

  • head

    JavaScript, Array

    Returns the head of an array.

  • tail

    JavaScript, Array

    Returns all elements in an array except for the first one.

  • last

    JavaScript, Array

    Returns the last element in an array.

  • flatten

    JavaScript, Array

    Flattens an array up to the specified depth.