Javascript 获取数组中的第一项和最后一项 - JS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43714111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:07:20  来源:igfitidea点击:

Get the first and last item in an Array - JS

javascriptarrays

提问by Gaara Itachi Uchiha

I am trying to get the first and last item in array and display them in an object.

我试图获取数组中的第一个和最后一个项目并将它们显示在一个对象中。

What i did is that I use the first and last function and then assign the first item as the key and the last item as the value.

我所做的是使用第一个和最后一个函数,然后将第一个项目指定为键,将最后一个项目指定为值。

var myArray = ['Rodel', 'Mike', 'Ronnie', 'Betus'];

function firstAndLast(array) {

var firstItem = myArray.first;
var lastItem = myArray.last;

 var objOutput = {
   firstItem : lastItem 
  };

}

var display = transformFirstAndLast(myArray);

console.log(display);

however this one gets me into trouble. It says undefined. Any idea why is that?

然而这个让我陷入困境。它说未定义。知道这是为什么吗?

回答by Ashraf

I've modified your code :

我已经修改了你的代码:

var myArray = ['Rodel', 'Mike', 'Ronnie', 'Betus'];

function firstAndLast(array) {

var firstItem = myArray[0];
var lastItem = myArray[myArray.length-1];

 var objOutput = {
   first : firstItem,
   last : lastItem
  };

return objOutput;
}

var display = firstAndLast(myArray);

console.log(display);

UPDATE: New Modification

更新:新的修改

var myArray = ['Rodel', 'Mike', 'Ronnie', 'Betus'];

function firstAndLast(array) {

var firstItem = myArray[0];
var lastItem = myArray[myArray.length-1];

var objOutput = {};
objOutput[firstItem]=lastItem

return objOutput;
}

var display = firstAndLast(myArray);

console.log(display);

回答by Kamil Kie?czewski

ES6

ES6

var objOutput = { [myArray[0]]: [...myArray].pop() }

var myArray = ['Rodel', 'Mike', 'Ronnie', 'Betus'];

var objOutput = { [myArray[0]]: [...myArray].pop() }

console.log(objOutput);

回答by roli roli

With ES6 and destructuring:

使用 ES6 和解构:

const myArray = ['Rodel', 'Mike', 'Ronnie', 'Betus'];

const { 0: first, length, [length -1]: last } = myArray //getting first and last el from array
const obj = { first, last }

console.log(obj) // {  first: "Rodel",  last: "Betus" }

回答by Felipe

well, I have another idea... for ex.:

好吧,我有另一个想法……例如:

const all = ['food', 'clean', 'cat', 'shower', 'work out']
console.log(`you have ${all.length} all!`)
console.log(`Todo: ${all[0]}`)
console.log(`Todo: ${all[all.length - 1]}`)

回答by Felipe

To make your firstand lastproperties work as you want, define them as properties with "getters" on the array prototype.

要使您的firstlast属性按您的需要工作,请将它们定义为在数组原型上带有“getter”的属性。

(You also have an inconsistency between firstAndLastArrayand transformFirstAndLast, which needed to be fixed.)

(您也有firstAndLastArray和之间的不一致transformFirstAndLast,需要修复。)

Returning {firstName: lastName}will not do what you presumably want, since it will yield the key firstName. To use the actual first name as the key, use computed property names({[firstName]: lastName}).

Returning{firstName: lastName}不会做你可能想要的,因为它会产生 key firstName。要使用实际名字作为键,请使用计算属性名称( {[firstName]: lastName})。

Object.defineProperties(Array.prototype, {
  first: { get() { return this[0]; }},
  last:  { get() { return this[this.length - 1]; }}
});

var myArray = ['Rodel', 'Mike', 'Ronnie', 'Betus'];

function firstAndLast(array) {
  var firstItem = myArray.first;
  var lastItem = myArray.last;
  
  return {[firstItem]: lastItem};
}

var display = firstAndLast(myArray);

console.log(display);

Or, much more simply, just

或者,更简单地说,只是

function firstAndLast(array) {
  return {[array[0]]: array[array.length - 1]};
}

If you don't want to, or cannot, use computed property names, then

如果您不想或不能使用计算属性名称,则

function firstAndLast(array) {
  var result = {};
  result[array[0]] = array[array.length - 1];
  return result;
}

回答by Deepak Dixit

Do like this :-

这样做:-

var myArray = ['Rodel', 'Mike', 'Ronnie', 'Betus'];

function firstAndLast(myArr) {

    var firstItem = myArr[0];
    var lastItem = myArr[myArr.length - 1];
    var objOutput = {}; 
    objOutput[firstItem] = lastItem;
    return objOutput;
}

var display = firstAndLast(myArray);

console.log(display);