javaScript set object key by variable

How to set an object key in JavaScript using a variable

If you want to set a JavaScript object key using a variable, there are multiple ways to achieve it. Let's explore some of them:

Method 1: Using bracket notation

One of the easiest ways to set an object key using a variable is by using bracket notation. This method is useful when you don't know the key name in advance.


let myObj = {};
let myKey = "name";
let myValue = "Raju";

myObj[myKey] = myValue;

console.log(myObj); // Output: { name: "Raju" }

In the above code, we're creating an empty object called `myObj`. Then we're creating two variables `myKey` and `myValue`, where `myKey` is the key name we want to set dynamically and `myValue` is the value we want to assign to the key.

We're then setting the value of `myObj[myKey]` to `myValue`. This sets the `name` key in `myObj` with the value `"Raju"`. Finally, we're logging the entire object to the console using `console.log()`.

Method 2: Using ES6 computed properties

Another way to set an object key using a variable is by using ES6 computed properties. This method is useful when you want to create an object with dynamic keys.


let myKey = "name";
let myValue = "Raju";

let myObj = {
  [myKey]: myValue
};

console.log(myObj); // Output: { name: "Raju" }

In the above code, we're using the ES6 computed properties syntax to create an object `myObj`. We're setting the key name dynamically by wrapping the variable `myKey` in square brackets.

We're then setting the value of the dynamically computed key to `myValue`. This creates an object with a single key-value pair `{ name: "Raju" }`. Finally, we're logging the entire object to the console using `console.log()`.

Method 3: Using Object.defineProperty()

You can also use the `Object.defineProperty()` method to set an object key using a variable. This method is useful when you want to define additional properties for the key.


let myObj = {};
let myKey = "name";
let myValue = "Raju";

Object.defineProperty(myObj, myKey, {
  value: myValue,
  writable: true,
  enumerable: true,
  configurable: true
});

console.log(myObj); // Output: { name: "Raju" }

In the above code, we're creating an empty object called `myObj`. Then we're creating two variables `myKey` and `myValue`, where `myKey` is the key name we want to set dynamically and `myValue` is the value we want to assign to the key.

We're then using the `Object.defineProperty()` method to define the property with the key name `myKey` and additional properties such as `value`, `writable`, `enumerable`, and `configurable`. This sets the `name` key in `myObj` with the value `"Raju"`. Finally, we're logging the entire object to the console using `console.log()`.

These are some of the ways to set an object key using a variable in JavaScript.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe