ES6 dynamic key (name) for object & object property assignment

Hands up! Or I will dynamically access your property.

Recently, I’ve picked up Javascript again at my day job writing software using ReactJS and Elixir. It had been a while I hadn’t code javascript professionally since AngularJS 1. Since 2014, there are so many new changes to the JS libraries and communities! I need to catch up.

Today I learned the new way in ES6 to use a dynamic key to access or assign an object property.

ES6 enables developers to create or access an object by dynamic keys or names:

You can think of many ways you can apply this to your coding scenario. How about the case where you might need to create an object with an increasing number in the key name?

Or, when you want to replace an item in an array with a dynamic index 😉

ES6's Object.assign function is used to copy the values of all of the enumerable own properties from one or more source objects to a target object

Read more about it here: https://www.ecma-international.org/ecma-262/6.0/#sec-object.assign