📄️ Array comparison
For simple array comparison you can use JSON stringify and compare the output strings:
📄️ Append / Prepend items to Array
Unshift
📄️ Converting Array-like Objects to Arrays
What are Array-like Objects?
📄️ Array spread / rest
Spread operator
📄️ Checking if an object is an Array
Array.isArray(obj) returns true if the object is an Array, otherwise false.
📄️ Concatenating Arrays
Two Arrays
📄️ Convert a String to an Array
The .split() method splits a string into an array of substrings. By default .split() will break the string into substrings on spaces (" "), which is equivalent to calling .split(" ").
📄️ Copy part of an Array
The slice() method returns a copy of a portion of an array.
📄️ Destructuring an array
Version ≥ 6
📄️ The entries() method
The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
📄️ filtering object arrays
Filtering Object Arrays
📄️ Filtering values
The filter() method creates an array filled with all array elements that pass a test provided as a function.
📄️ finding the min or max element
Finding the minimum or maximum element
📄️ Flattening Arrays
2 Dimensional arrays
📄️ Insert an item into an array at a specific index
Simple item insertion can be done with Array.prototype.splice method:
📄️ Iteration
A traditional for-loop
📄️ logical connective of values
Version ≥ 5.1
📄️ Mapping Values
It is often necessary to generate a new array based on the values of an existing array.
📄️ Merge two array as key value pair
When we have two separate array and we want to make key value pair from that two array, we can use array's reduce function like below:
📄️ Object keys and values to array
Now array is
📄️ Reducing values
The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
📄️ Remove value from array
When you need to remove a specific value from an array, you can use the following one-liner to create a copy array without the given value:
📄️ Removing all elements
Method 1
📄️ Removing duplicate elements
From ES5.1 onwards, you can use the native method Array.prototype.filter to loop through an array and leave only entries that pass a given callback function.
📄️ Removing items from an array
Shift
📄️ Removing/Adding elements using splice()
The splice() method can be used to remove elements from an array. In this example, we remove the first 3 from the array.
📄️ Reversing arrays
.reverse is used to reverse the order of items inside an array.
📄️ Searching an Array
The recommended way (Since ES5) is to use Array.prototype.find:
📄️ Shallow cloning an array
Sometimes, you need to work with an array while ensuring you don't modify the original. Instead of a clone method, arrays have a slice method that lets you perform a shallow copy of any part of an array. Keep in mind that this only clones the first level. This works well with primitive types, like numbers and strings, but not objects.
📄️ sorting arrays
Sorting Arrays
📄️ Sorting multidimensional array
Given the following array
📄️ Standard array initialization
There are many ways to create arrays. The most common are to use array literals, or the Array constructor:
📄️ Test all array items for equality
The .every method tests if all array elements pass a provided predicate test.