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 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), {});

Sep 9, 2021 JavaScript
How to count the occurrences of array elements in JavaScript

In this Article we will go through how to count the occurrences of array elements 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 countOccurrences = arr => arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {});

Sep 9, 2021 JavaScript
How to create an array of cumulative sum in JavaScript

In this Article we will go through how to create an array of cumulative sum 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 accumulate = arr => arr.map((sum => value => sum += value)(0));

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

In this Article we will go through how to flatten 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 flat = arr => [].concat.apply([], arr.map(a => Array.isArray(a) ? flat(a) : a));

Sep 9, 2021 JavaScript
How to get all n-th items of an array in JavaScript

In this Article we will go through how to get all n-th items of 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 getNthItems = (arr, nth) => arr.filter((_, i) => i % nth === nth - 1);

Sep 9, 2021 JavaScript
How to get the intersection of arrays in JavaScript

In this Article we will go through how to get the intersection of 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 getIntersection = (a, ...arr) => [...new Set(a)].filter(v => arr.every(b => b.includes(v)));

Sep 9, 2021 JavaScript
How to get the rank of an array of numbers in JavaScript

In this Article we will go through how to get the rank of 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 ranking = (arr) => arr.map((x, y, z) => z.filter(w => w > x).length + 1);

Sep 9, 2021 JavaScript
How to get union of arrays in JavaScript

In this Article we will go through how to get union of 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 union = (...arr) => [...new Set(arr.flat())];

Sep 9, 2021 JavaScript
How to group an array of objects by a key in JavaScript

In this Article we will go through how to group an array of objects by a 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 groupBy = (arr, key) => arr.reduce((acc, item) => ((acc[item[key]] = [...(acc[item[key]] || []), item]), acc), {});