Some diversity of approaches (admittedly esoteric, but fun!):
function firstAndLast(a, o){
return !(o = {}, o[a.shift()] = a.pop()) || o;
}
console.log(firstAndLast([1,2,3,4,5]));
console.log(firstAndLast(['a','to','z']));
https://jsfiddle.net/brtsbLp1/
And, of course:
function firstAndLast(a, o){
return !(o = o || {}, o[a.shift()] = a.pop()) || o;
}
console.log(firstAndLast(['a','to','z']));
console.log(firstAndLast(['a','to','z'], {a:'nother',obj:'ect'}));
https://jsfiddle.net/brtsbLp1/1/
Another fun one:
function firstAndLast(a){
return JSON.parse('{"'+a.shift()+'":"'+a.pop()+'"}');
}
https://jsfiddle.net/brtsbLp1/2/
That last one will choke on the first being a number (since labels aren’t allowed to be numbers only), plus other issues in general. But this should give some food for thought. The other answers are a bit more obvious.
3
solved Arrays to objects