i.e.:
assert(createObj('foo', 5)) == {foo: 5}
One implementation would of course be:
function createObj(key, val) {
var ret = {};
ret[key] = val;
return ret;
}
Just wondering if there's a one-liner.
i.e.:
assert(createObj('foo', 5)) == {foo: 5}
One implementation would of course be:
function createObj(key, val) {
var ret = {};
ret[key] = val;
return ret;
}
Just wondering if there's a one-liner.
There is, in ES2015:
{
['foo']: 123
}
And there is no really simple and "fair" one in ES5.1
References:
It looks like is not possible in pure JavaScript. You need to make the object first, then use [] to set it.
However, is possible in frameworks. For instance, in ES.