Sep 9, 2021 JavaScript
How to shuffle an array in JavaScript

In this Article we will go through how to shuffle an array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const shuffle = arr => arr.map(a => ({ sort: Math.random(), value: a })).sort((a, b) => a.sort - b.sort).map(a => a.value);

Sep 9, 2021 JavaScript
How to sort an array of items by given key in JavaScript

In this Article we will go through how to sort an array of items by given key only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const sortBy = (arr, k) => arr.concat().sort((a, b) => (a[k] > b[k]) ? 1 : ((a[k] < b[k]) ? -1 : 0));

Sep 9, 2021 JavaScript
How to sort an array of numbers in JavaScript

In this Article we will go through how to sort an array of numbers only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const sort = arr => arr.sort((a, b) => a - b);

Sep 9, 2021 JavaScript
How to split an array into chunks in JavaScript

In this Article we will go through how to split an array into chunks only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const chunk = (arr, size) => arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);

Sep 9, 2021 JavaScript
How to clone an array in JavaScript

In this Article we will go through how to clone an array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const clone = arr => arr.slice(0);

Sep 9, 2021 JavaScript
How to compare two arrays regardless of order in JavaScript

In this Article we will go through how to compare two arrays regardless of order only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const isEqual = (a, b) => JSON.stringify(a.sort()) === JSON.stringify(b.sort());

Sep 9, 2021 JavaScript
How to compare two arrays in JavaScript

In this Article we will go through how to compare two arrays only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);

Sep 9, 2021 JavaScript
How to convert an array of objects to a single object in JavaScript

In this Article we will go through how to convert an array of objects to a single object only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const toObject = (arr, key) => arr.reduce((a, b) => ({ ...a, [b[key]]: b }), {});

Sep 9, 2021 JavaScript
How to convert an array of strings to numbers in JavaScript

In this Article we will go through how to convert an array of strings to numbers only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const toNumbers = arr => arr.map(Number);

Sep 9, 2021 JavaScript
How to count by the properties of an array of objects in JavaScript

In this Article we will go through how to count by the properties of an array of objects only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const countBy = (arr, prop) => arr.reduce((prev, curr) => (prev[curr[prop]] = ++prev[curr[prop]] || 1, prev), {});