JavaScript Array Snippets

The JavaScript snippet collection contains a wide variety of ES6 helper functions. It includes helpers for dealing with primitives, arrays and objects, as well as algorithms, DOM manipulation functions and Node.js utilities.

  • Generate items

    Generates an array with the given amount of items, using the given function.

    JavaScript, Array · Oct 22, 2020

  • Initialize 2D array

    Initializes a 2D array of given width and height and value.

    JavaScript, Array · Oct 20, 2020

  • Array intersection

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

    JavaScript, Array · Oct 20, 2020

  • Nth element

    Returns the nth element of an array.

    JavaScript, Array · Oct 21, 2020

  • Insertion index in sorted array

    Finds the lowest index at which a value should be inserted into an array in order to maintain its sorting order.

    JavaScript, Array · Oct 22, 2020

  • Transpose matrix

    Transposes a two-dimensional array.

    JavaScript, Array · Apr 20, 2022

  • Filter out matching array elements

    Filters out the elements of an array that have one of the specified values.

    JavaScript, Array · Oct 22, 2020

  • First n elements

    Gets the first n elements of an array.

    JavaScript, Array · Jul 22, 2022

  • Last n elements

    Gets the last n elements of an array.

    JavaScript, Array · Jul 23, 2022

  • Last insertion index in sorted array

    Finds the highest index at which a value should be inserted into an array in order to maintain its sort order.

    JavaScript, Array · Oct 22, 2020

  • Unique values in array

    Finds all unique values in an array.

    JavaScript, Array · Oct 22, 2020

  • Convert function to variadic

    Changes a function that accepts an array into a variadic function.

    JavaScript, Function · Jun 13, 2021

  • Check if array has duplicates

    Checks if there are duplicate values in a flat array.

    JavaScript, Array · Oct 22, 2020

  • Check if two arrays intersect

    Determines if two arrays have a common item.

    JavaScript, Array · Feb 17, 2023

  • Array is sorted

    Checks if a numeric array is sorted.

    JavaScript, Array · Oct 20, 2020