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 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 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 remove falsy values from array in JavaScript

In this Article we will go through how to remove falsy values from 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 removeFalsy = arr => arr.filter(Boolean);

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

In this Article we will go through how to shuffle 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 shuffle = arr => arr.map(a => ({ sort: Math.random(), value: a })).sort((a, b) => a.sort - b.sort).map(a => a.value);

Sep 9, 2021 JavaScript
How to sort an array of items by given key in JavaScript

In this Article we will go through how to sort an array of items 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 sortBy = (arr, k) => arr.concat().sort((a, b) => (a[k] > b[k]) ? 1 : ((a[k] < b[k]) ? -1 : 0));

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 get the sum of an array of numbers in JavaScript

In this Article we will go through how to get the sum of 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 sum = arr => arr.reduce((a, b) => a + b, 0);