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;

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 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 maximum item of an array in JavaScript

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

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 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 index of the minimum item of an array in JavaScript

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

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