Computed properties in JavaScript

When declaring an object, the names of its properties can be taken from variables. Such properties are called computed. Let's see how it's done. Let's have the following object:

let obj = { a: 1, b: 2, c: 3 };

Let the name of some property be stored in a variable:

let key = 'a';

Let's make it such a way that instead of the property name, the value from our variable is taken. To do this, the variable should be enclosed in square brackets:

let obj = { [key]: 1, b: 2, c: 3 };

When declaring computed properties, you can execute some code. An example:

let obj = { [key + '1']: 1, [key + '2']: 2, [key + '3']: 3 };

In the following code, the key should have been taken from a variable. Fix the mistake you made:

let key = 'x'; let obj = { key: 1, y: 2, z: 3 };

Given an object:

let obj = { x: 1, y: 2, z: 3 };

Given variables:

let key1 = 'x'; let key2 = 'y'; let key3 = 'z';

Make the object keys to be taken from these variables.

enru