Sep 9, 2021 JavaScript
How to truncate a string at full words in JavaScript

In this Article we will go through how to truncate a string at full words 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 truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(0, str.substr(0, max - suffix.length).lastIndexOf(' '))}${suffix}`;

Sep 9, 2021 JavaScript
How to get union of arrays in JavaScript

In this Article we will go through how to get union 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 union = (...arr) => [...new Set(arr.flat())];

Sep 9, 2021 JavaScript
How to group an array of objects by a key in JavaScript

In this Article we will go through how to group an array of objects by a 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 groupBy = (arr, key) => arr.reduce((acc, item) => ((acc[item[key]] = [...(acc[item[key]] || []), item]), acc), {});

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