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 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 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 remove duplicate values in an array in JavaScript

In this Article we will go through how to remove duplicate values 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 removeDuplicate = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));

Sep 9, 2021 JavaScript
How to sort an array of numbers in JavaScript

In this Article we will go through how to sort an array of 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 sort = arr => arr.sort((a, b) => a - b);

Sep 9, 2021 JavaScript
How to split an array into chunks in JavaScript

In this Article we will go through how to split an array into chunks 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 chunk = (arr, size) => arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);

Sep 9, 2021 JavaScript
How to swap the rows and columns of a matrix in JavaScript

In this Article we will go through how to swap the rows and columns of a matrix 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 transpose = matrix => matrix[0].map((col, i) => matrix.map(row => row[i]));

Sep 9, 2021 JavaScript
How to unzip an array of arrays in JavaScript

In this Article we will go through how to unzip an array 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 unzip = arr => arr.reduce((acc, c) => (c.forEach((v, i) => acc[i].push(v)), acc), Array.from({ length: Math.max(...arr.map(a => a.length)) }, (_) => []));

Sep 9, 2021 JavaScript
How to zip multiple arrays in JavaScript

In this Article we will go through how to zip multiple 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 zip = (...arr) => Array.from({ length: Math.max(...arr.map(a => a.length)) }, (_, i) => arr.map(a => a[i]));

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