DEV Community

JavaScript Array Guides

Let's get to know the functionality of the JavaScript array object. Hopefully you find these guides helpful.

This is a collection of top and trending guides written by the community on subjects related to JavaScript Array concepts. For all things JavaScript, check out the JavaScript tag! Please contribute more posts like this to help your fellow developer in need.

How to convert an array into an object in javascript

To convert an array into an object we will create a function and give it 2 properties, an array and a key.


Removing duplicates in an Array of Objects in JS with Sets

I was getting from my async http call an array of objects (addresses in this case), but for unimportant reasons this array was returning objects that could be duplicate (so two identical ids).


How to Deep Clone an Array in JavaScript

There are 2 types of array cloning: shallow & deep. Shallow copies only cover the 1st level of the array and the rest are referenced. If you want a true copy of nested arrays, you’ll need a deep clone. For deep clones, go with the JSON way OR better yet use Lodash 👍


How to create an array of unique values in JavaScript using Sets

Imagine you have a JavaScript array that contains many elements, some of which are duplicated:


React: how to dynamically sort an array of objects using the dropdown (with React Hooks)

The sample array of objects:


JavaScript: Map an array of objects to a dictionary

The easiest way for converting an array of objects to a dictionary in JavaScript / TypeScript:


Dealing with Promises In an Array with async/await

Promise.all accepts an array of promises and returns a new promise that resolves only when all of the promises in the array have been resolved. The promise resolves to an array of all the values that the each of the promise returns.


3 ways to remove duplicates in an Array in Javascript

To eliminate duplicates, the filter() method is used to include only the elements whose indexes match their indexOf values, since we know that the filer method returns a new array based on the operations performed on it:


The JavaScript Array Map() Method

Ever wondered what the JavaScript array map method is? what it can do? or when it should be used? This is article is for you. Before we go any further, let us get a formal definition of the map method.


How to shuffle an array in JavaScript

In this article we’ll take a look at a couple of ways to shuffle an array in JavaScript.


JavaScript: How to Check if an Array has Duplicate Values

When dealing with arrays of values in JavaScript we sometimes want to determine if the array contains any duplicate values. Unfortunately, JavaScript arrays do not expose any built-in methods that can do this for us -- we have to write the implementation ourselves.


6 formas de convertir un String en Array con Javascript

Una cadena de texto en javascript puede ser convertida en Array con 6 métodos diferentes.


Quick Tip: Transform an Array into an Object using .reduce()

Let's say you have an array like this:


10 JavaScript array methods you should know

In this post i will share 10 JavaScript array methods you should know.


Find and Replace elements in Array with JavaScript

First, let's look at different ways of checking if our Array includes a certain value provided.


How to make a real copy of a JavaScript Array with Objects (without a reference)

It happens a lot that you want to copy a variable, with an Object or Array, to manipulate the data and use it afterward. But when we create an array of users and make a copy in a new variable, like this:


Filter an array for unique values in Javascript

Set is a new data structure introduced in ES6. It is somewhat similar to an array but it does not allow us to store duplicate values.


Moving Element In An Array From Index To Another

I was working on a project when I faced an unprecedented and obvious issue. How do I suppose to move an element in an array form one position to another?


function to check if all records are equal in array | javascript

Here, this function will get an array from parameter and in return, it’s using every ( method of the array ) to get individual records to compare it with the 0th index record. it’ll return true if all records are the same as the 0th index record or else false. Now, let’s look at the results…


Convert an array of objects to CSV string in JavaScript

Naturally, there are already packages out there that can handle a variety of CSV use cases, but we just wanted a simple solution in JavaScript as we knew that the data structure coming from our database was always going to be an array of objects.


UseEffect dependency array and object comparison!

If you are performing any side effects in your function, the Effect hook have to be there. This useEffect hook takes first parameter as a function to perform side effect and second parameter, a dependencies array. If you do not wish to perform side effects on every render (which is the case almost every time), you need to pass something to this dependency array or at least an empty array. This array will re-run useEffect, if the values inside it changes. This will work perfectly fine when the values passed in the dependency array are of type boolean, string or numbers. But it will have some gotchas when you are dealing with complex values such as objects or arrays.


Time complexity Big 0 for Javascript Array methods and examples.

Hello everyone, some weeks ago I started to study some computer science algorithms using JavaScript as the programming language, and normally after finished to implement an algorithm I like to calculated complexity with the Big 0 notation. That is the reason why I wanted to write this post, to understand the time complexity for the most used JS Array methods.


How to update object or array state in React

Ever tried to update object or array state directly in React?

I did that, the state of my component didn't change.

Destructuring the object/array was the solution.


JS: Sort an Array of Objects on multiple columns/keys

Let's see how you can completely configure the sorting of an Array of objects. Let's say we have the below data set for our entire example.


How to update an array state in React.Js

Ever tried to update an array in the React and ended up mutating the existing state?

Then you might be wondering why your component didn't update. Well you are not alone i did that too, and turns out a lot of other people face the exact same issue.


How To Immutably Update An Array In TypeScript

Using an array to collect objects is useful. But, it presents some challenges with searching and updating the data. In React, updating an object in an array will not cause re-renders, adding more complication.


Solution: Minimum Moves to Equal Array Elements II

Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.


Two Ways to Rotate an Array in JavaScript

Often writing a seemingly straightforward array or string algorithm will trip us up, due to us overcomplicating things or simply not knowing some of the more fundamental building blocks of working with those data types.


Two dimensional array search

A friend posed a quandary. What's the cleanest way of searching a two-dimensional array in JavaScript for an element and returning the index of its containing array?


Cool trick to filter undefined values from an array in Javascript

I came across a similar situation not too long ago where someone needed to filter undefined objects from an array and used this mysterious syntax to do so..


Finding an element in the array (the ES5, ES6 and ES7 way)

The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.


JavaScript Array Functions CheatSheet

An in my opinion better-looking version of this cheat sheet can be found on my website: wweb.dev/resources/js-array-functions-cheatsheet :)


.map( ) .forEach( ) for( ). 👉 Three ways for create an array and push it:

I want to show you 3 different way for push a value intro your array empty, with the same result.


To pass MobX's observable array to FlatList, .slice() is needed

This may be because FlatList receives array and render lists.

Exact Prop type Array is unexpected for FlatList.


Simplest way to compare two numbers array in JS

But what if we convert our array to string? Then you can use the comparison operator. This makes the task very easy. We can sort an array using toString method eg. array1.toString() or we can use this hack


JavaScript Katas: Split a number array into odd and even numbers

Write a function splitOddAndEven, that accepts one parameter: numbers, an array of positive numbers.


Using ES6 array destructing with Promises

As you know, some months ago, it was announced the new features of ES6 or EcmaScript2015. In the beginning not all of them were implemented by some browsers or Node.js, but we had tools like Babel.js to allow us to program in ES6 and then it would translate the code to ES5.

I am a Node.js developer and when I read the list of new features of ES6, there was one of them that could help me to continue fighting against callback hell and improve the readability of Promises. In particular, the readability of Promise.all() that, as we know, launches an array of promises in parallel. This feature is array destructing.


Typeof array is an object in javascript

Apparently there seems to be something wrong because the array is recognized as an object and seems to be no real difference between object and array.

This because in javascript all derived data type is always a type object. Included functions and array.

In case you need to check if it’s an array you can use isArray method of Array.


Sorting an array into groups with reduce

The reduce function iterates each element of the array and passes the return object to the next iteration. The accumulator (named groups here) is set initially as a {} empty object.


Update an object in nested array in MongoDB

To update an object inside a nested array in MongoDB you can use this approach, Its well tested and working.


How to get the highest and lowest number from an array in JavaScript?

To get the highest or lowest number from an array in JavaScript, you can use the Math.max() or the Math.min() methods then spread the elements from the array to these methods using the spread operator (...).


How to sort array of object using object keys in JavaScript?

Here we are going to sort an array of objects using keys that are available in that objects.


Using array map, filter and reduce in MongoDB aggregation pipeline

If you work with javascript, the chances of you are using array methods like map, filter and reducer today are really great.


Higher Order Array Methods in JavaScript

This is one of the simplest functions you are going to use while working with Arrays. It forms a new array by calling the function passed into it as an argument, on each and every element of the Array. It will map each of the return values of the callback and create a new array.


How To Check If Array Is Empty In TypeScript

When processing JSON responses in TypeScript, how do you safely check if a variable is an array and is not empty?


JavaScript array push, pop, shift, unshift: how to remember the difference

Unshift and shift make the whole array shift sideways (by adding and removing items from the beginning).


JavaScript Array methods: Filter, Map, Reduce, and Sort

Arrays provide a lot of methods. JavaScript already has methods built into its Array data type. Follows the examples of how to use it.


Happy JavaScript Array coding!