In this Article we will go through how to compare two arrays regardless of order 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.sort()) === JSON.stringify(b.sort());
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), {});
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), {});
In this Article we will go through how to create an array of cumulative sum 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 accumulate = arr => arr.map((sum => value => sum += value)(0));
In this Article we will go through how to flatten 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 flat = arr => [].concat.apply([], arr.map(a => Array.isArray(a) ? flat(a) : a));
In this Article we will go through how to get all n-th items 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 getNthItems = (arr, nth) => arr.filter((_, i) => i % nth === nth - 1);
In this Article we will go through how to get the intersection 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 getIntersection = (a, ...arr) => [...new Set(a)].filter(v => arr.every(b => b.includes(v)));
In this Article we will go through how to get the rank 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 ranking = (arr) => arr.map((x, y, z) => z.filter(w => w > x).length + 1);
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())];
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), {});