Sep 9, 2021 JavaScript
How to extract values of a property from an array of objects in JavaScript

In this Article we will go through how to extract values of a property from 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 pluck = (objs, property) => objs.map(obj => obj[property]);

Sep 9, 2021 JavaScript
How to get the value at given path of an object in JavaScript

In this Article we will go through how to get the value at given path of an 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 getValue = (path, obj) => path.split('.').reduce((acc, c) => acc && acc[c], obj);

Sep 9, 2021 JavaScript
How to immutably rename object keys in JavaScript

In this Article we will go through how to immutably rename object keys 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 renameKeys = (keysMap, obj) => Object.keys(obj).reduce((acc, key) => ({ ...acc, ...{ [keysMap[key] || key]: obj[key] }}), {});

Sep 9, 2021 JavaScript
How to check if a rectangle overlaps other one in JavaScript

In this Article we will go through how to check if a rectangle overlaps other one 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:

// Returns true if `a` overlaps `b`
// (x1, y1) and (x2, y2) are top-left and bottom-right corners
const overlaps = (a, b) => (a.x1 < b.x2 && b.x1 < a.x2) || (a.y1 < b.y2 && b.y1 < a.y2);

Sep 9, 2021 JavaScript
How to convert degrees to radians in JavaScript

In this Article we will go through how to convert degrees to radians 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 degsToRads = deg => deg * Math.PI / 180.0;

Sep 9, 2021 JavaScript
How to convert radians to degrees in JavaScript

In this Article we will go through how to convert radians to degrees 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 radsToDegs = rad => rad * 180 / Math.PI;

Sep 9, 2021 JavaScript
How to normalize the ratio of a number in a range in JavaScript

In this Article we will go through how to normalize the ratio of a number in a range 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 normalizeRatio = (value, min, max) => (value - min) / (max - min);

Sep 9, 2021 JavaScript
How to round a number to the nearest multiple of a given value in JavaScript

In this Article we will go through how to round a number to the nearest multiple of a given value 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 roundNearest = (value, nearest) => Math.round(value / nearest) * nearest;

Sep 9, 2021 JavaScript
How to check if the code is running in nodejs in JavaScript

In this Article we will go through how to check if the code is running in nodejs only using single line of code in JavaScript.

Let's define this short function:

const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;

Sep 9, 2021 JavaScript
How to check if the code is running in the browser in JavaScript

In this Article we will go through how to check if the code is running in the browser only using single line of code in JavaScript.

Let's define this short function:

const isBrowser = typeof window === 'object' && typeof document === 'object';