I am using a database and I want to store items that a user has in an object. How would I push a variable's value into a name of an object's value?
Code example:
let item = 'test'
items.push({item: other})
I am using a database and I want to store items that a user has in an object. How would I push a variable's value into a name of an object's value?
Code example:
let item = 'test'
items.push({item: other})
You can set the key of an object to be the value of a variable like this:
let item = 'test';
items.push({[item]: "foobar"});
Your result will look like this: {test: "foobar"}
Is this what you are looking for?
let item = "test";
let items = [];
items.push({
[item]: "other"
})
console.log(items);