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

In this Article we will go through how to empty 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 empty = arr => arr.length = 0;

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

Sep 9, 2021 JavaScript
How to count the occurrences of a value in an array in JavaScript

In this Article we will go through how to count the occurrences 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 countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);

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