I'm trying to convert an array to an object (keyed by the first element).
foo = [1,2]
function convert_foo(foo) {
return { foo[0]: foo[1] };
}
The following is not valid Javascript: Uncaught SyntaxError: Unexpected token [.
I've also tried:
function convert_foo(foo) {
return ({ foo[0]: foo[1] });
}
EDIT:
It's possible this way, but I was wondering if there was a way to return it in one line.
function convert_foo(foo) {
var obj = {}
obj[foo[0]] = foo[1];
return obj;
}