Sep 9, 2021 JavaScript
How to create an array of numbers in the given range in JavaScript

In this Article we will go through how to create an array of numbers in the given range 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 range = (min, max) => [...Array(max - min + 1).keys()].map(i => i + min);

Sep 9, 2021 JavaScript
How to create cartesian product in JavaScript

In this Article we will go through how to create cartesian product 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 cartesian = (...sets) => sets.reduce((acc, set) => acc.flatMap((x) => set.map((y) => [...x, y])), [[]]);

Sep 9, 2021 JavaScript
How to find the closest number from an array in JavaScript

In this Article we will go through how to find the closest number from 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:

// Find the number from `arr` which is closest to `n`
const closest = (arr, n) => arr.reduce((prev, curr) => Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev);

Sep 9, 2021 JavaScript
How to find the index of the last matching item of an array in JavaScript

In this Article we will go through how to find the index of the last matching item 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 lastIndex = (arr, predicate) => arr.reduce((prev, curr, index) => predicate(curr) ? index : prev, -1);

Sep 9, 2021 JavaScript
How to find the maximum item of an array by given key in JavaScript

In this Article we will go through how to find the maximum item of an array 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 maxBy = (arr, key) => arr.reduce((a, b) => a[key] >= b[key] ? a : b, {});

Sep 9, 2021 JavaScript
How to find the length of the longest string in an array in JavaScript

In this Article we will go through how to find the length of the longest string 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 findLongest = words => Math.max(...(words.map(el => el.length)));

Sep 9, 2021 JavaScript
How to find the index of the maximum item of an array in JavaScript

In this Article we will go through how to find the index of the maximum item 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 indexOfMax = arr => arr.reduce((prev, curr, i, a) => curr > a[prev] ? i : prev, 0);

Sep 9, 2021 JavaScript
How to find the minimum item of an array by given key in JavaScript

In this Article we will go through how to find the minimum item of an array 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 minBy = (arr, key) => arr.reduce((a, b) => a[key] < b[key] ? a : b, {});

Sep 9, 2021 JavaScript
How to find the minimum item of an array in JavaScript

In this Article we will go through how to find the minimum item 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 min = arr => Math.min(...arr);

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));