Im trying to have something liek this:
let temp={`${otherName}`: `${otherValue}`}
but I get alot of errors with it.
I expected no errors and I know its a typo but ive searched for a soulision for 1 hour. I tried different quotes.
code:
const otherName = document.getElementById(`adminPanelManageUserOtherName`).value;
const otherValue = document.getElementById(`adminPanelManageUserOtherValue`).value;
let temp={`${otherName}`: `${otherValue}`}
2
Answers
You could create
temp
first and then use template strings to assign values to it:there are multiple ways to implement what you want. The most basic implementation is by using
computed property key
the square brackets syntax allows you to dynamically make a key based on the variable you put inside.
If for some reasons you need to dynamically add multiple values in an object (since from what you’re doing you’re getting values from an element), then you can probably use this:
this is useful for loops or adding multiple key-value pairs.