How to instantiate a object with a variable key-value pair in Javascript?

157 Views Asked by At

I want to instantiate a object with its key being a variable. Something along the lines of this:

var deleteDuplicates = function(head) {
    let dict = {
        `${head.val}`: 1
    }
}

Is something like this possible besides having another line write to it:

let dict = {}
if (head) dict[head.val] = 1
1

There are 1 best solutions below

0
On

The syntax for throwing a variable in as a key is:

let dict = {
   [head.val]: 1
}

Source: ES6