JSFiddle - React, Tailwind, and code Playground

JavaScript

function firstAndLast(a, o){
    return !(o = o || {}, o[a.shift()] = a.pop()) || o;
}

console.log(firstAndLast([1,2,3,4,5]));
console.log(firstAndLast(['a','to','z']));
console.log(firstAndLast(['a','to','z'], {a:'nother',obj:'ect'}));