In this Article we will go through how to check if an array is empty 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 isEmpty = arr => !Array.isArray(arr) || arr.length === 0;
In this Article we will go through how to clone 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 clone = arr => arr.slice(0);
In this Article we will go through how to cast a value as 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 castArray = value => Array.isArray(value) ? value : [value];
In this Article we will go through how to compare 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:
const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
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 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 }), {});