我需要构建一个可以接收数组的函数,并且在不更改数组的情况下,返回一个对象,该对象的属性是该数组的第一个元素,该属性的值是数组的最后一个元素:
示例:
var array = ["Orange", "glass", "hat", "juicy", "green"]
function firstAndLast(array){
}
//output should be:
{
Orange : 'green'
}
非常感谢您的帮助:)
答案 0 :(得分:0)
实际上非常简单
var array = ["Orange", "glass", "hat", "juicy", "green"];
// take the first elem and map it to the last element
// the index of the last element being array.length - 1
var res = {[array[0]] : array[array.length - 1]};
console.log(res);

答案 1 :(得分:0)
你去吧
var arr= ["Orange", "glass", "hat", "juicy", "green"]
function firstAndLast(arr){
return {[arr[0]] : arr[arr.length - 1]}
}
结果
{ Orange : 'green' }
答案 2 :(得分:0)
var array = ["Orange", "glass", "hat", "juicy", "green"];
var myelem = {[array[0]] : array[array.length-1]};
console.log(myelem);