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 repeat an array in JavaScript

In this Article we will go through how to repeat 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 repeat = (arr, n) => [].concat(...Array(n).fill(arr));

Sep 9, 2021 JavaScript
How to swap two array items in JavaScript

In this Article we will go through how to swap two array items 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 swapItems = (a, i, j) => a[i] && a[j] && [...a.slice(0, i), a[j], ...a.slice(i + 1, j), a[i], ...a.slice(j + 1)] || a;

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

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