Sep 9, 2021 JavaScript
How to get the unique values of an array in JavaScript

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

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

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

In this Article we will go through how to merge 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:

// Merge but don't remove the duplications
const merge = (a, b) => a.concat(b);

Sep 9, 2021 JavaScript
How to partition an array based on a condition in JavaScript

In this Article we will go through how to partition an array based on a condition 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 partition = (arr, criteria) => arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]);

Sep 9, 2021 JavaScript
How to get all arrays of consecutive elements in JavaScript

In this Article we will go through how to get all arrays of consecutive 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 getConsecutiveArrays = (arr, size) => size > arr.length ? [] : arr.slice(size - 1).map((_, i) => arr.slice(i, size + i));

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 all subsets of an array in JavaScript

In this Article we will go through how to get all subsets 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 getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]);

Sep 9, 2021 JavaScript
How to get indices of a value in an array in JavaScript

In this Article we will go through how to get indices of a value in 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 indices = (arr, value) => arr.reduce((acc, v, i) => (v === value ? [...acc, i] : acc), []);

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

In this Article we will go through how to get the average 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 average = arr => arr.reduce((a, b) => a + b, 0) / arr.length;