Destructuring Nested objects in javascript | Destructure second level parent and child Objects

child: { title2 } is just destructuring the child property. If you want to pick up the child property itself simply specify it in the statement: let { title, child, child: { title2 } } = obj1; const obj1 = { title: ‘foo’, child: { title2: ‘bar’ } } let { title, child, child: { … Read more

What is the difference between const and const {} in JavaScript

The two pieces of code are equivalent but the first one is using the ES6 destructuring assignment to be shorter. Here is a quick example of how it works: const obj = { name: “Fred”, age: 42, id: 1 } //simple destructuring const { name } = obj; console.log(“name”, name); //assigning multiple variables at one … Read more

ES6 destructuring object assignment function parameter default value

If you use it, and call the function with no parameters, it works: function drawES6Chart({size=”big”, cords = { x: 0, y: 0 }, radius = 25} = {}) { console.log(size, cords, radius); // do some chart drawing } drawES6Chart(); if not, an error is thrown: TypeError: can’t convert undefined to object function drawES6Chart({size=”big”, cords = … Read more

ES6 Destructuring and Module imports

import {Link} from ‘react-router’; imports a named export from react-router, i.e. something like export const Link = 42; import Router from ‘react-router’; const {Link} = Router; pulls out the property Link from the default export, assuming it is an object, e.g. export default { Link: 42 }; (the default export is actually nothing but a … Read more