Edit
My question is different than the duplicate, because I'm not asking why something works and something else doesn't, I'm asking how to do something. While both questions have the same answer doesn't mean they are the same.
I am trying to create an object for a Google Chrome Extension using the following function:
function set(key, value) {
chrome.storage.local.set({`${key}`:value}, function() {
console.log('Value is set to ' + value);
});
}
For some reason, this doesn't work.
Some things that I have already tried are shown below:
function set(key, value) {
chrome.storage.local.set({[`${key}`]=value, function() {
console.log('Value is set to ' + value);
});
}
function set(key, value) {
chrome.storage.local.set({key:value}, function() {
console.log('Value is set to ' + value);
});
}
function set(key, value) {
chrome.storage.local.set({"'"+key+"'":value}, function() {
console.log('Value is set to ' + value);
});
}
None of which seem to work.
What I would like to happen is to be able to set any key to any value inside of the chrome extension local memory using a function.
Expected Code:
set('testKey', 'testValue')
Expected Output:
Value is set to testValue
Actual Output:
Uncaught SyntaxError: Unexpected template string