Javascript 从数组中获取第一个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35605548/
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 17:58:22  来源:igfitidea点击:

Get first object from array

javascriptarrays

提问by Conor Shannon

I am trying to create an array which will hold just the Name elements from this array:

我正在尝试创建一个仅包含此数组中的 Name 元素的数组:

var array = [{Name: "steve"}, {Age: 18}, {Location: "Uk"}];

I am new to JavaScript and I am not sure how this would be done.

我是 JavaScript 的新手,我不确定这将如何完成。

回答by tlebrize

Here is a good read to understand how object works: http://www.w3schools.com/js/js_objects.asp

这是了解对象如何工作的好读物:http: //www.w3schools.com/js/js_objects.asp

if you really want the name first element from this array just use

如果你真的想要这个数组中的第一个元素的名称,只需使用

array[0]

回答by twernt

If you want an array of just the objects that have a Namekey, you can use Array.prototype.filter().

如果你想要一个只有具有Name键的对象的数组,你可以使用Array.prototype.filter()

This will return a two-item array [{Name: "steve"}, {Name: "conor"}]:

这将返回一个包含两项的数组[{Name: "steve"}, {Name: "conor"}]

var array = [{Name: "steve"}, {Age: 18}, {Location: "Uk"}, 
             {Name: "conor"}, {Age: 18}, {Location: "Uk"}];

var names = array.filter(function(obj) {
  if ('Name' in obj) {
    return true;
  } else {
    return false;
  }
});

If you want an array of just the Namevalues of just the objects that have a Namekey, you can use Array.prototype.filter()and Array.prototype.map()together.

如果你想要一个只有Name具有Name键的对象的值的数组,你可以一起使用Array.prototype.filter()Array.prototype.map()

This will return a two-item array ["steve", "conor"]:

这将返回一个包含两项的数组["steve", "conor"]

var array = [{Name: "steve"}, {Age: 18}, {Location: "Uk"}, 
             {Name: "conor"}, {Age: 18}, {Location: "Uk"}];

var names = array.filter(function(obj) {
  if ('Name' in obj) {
    return true;
  } else {
    return false;
  }
}).map(function(obj) { return obj['Name']; });

Either way, you may want to take another look at the structure of your array. It probably makes more sense to group your "people" so that each one is a single object, something like:

无论哪种方式,您可能都想再看看数组的结构。将您的“人”分组可能更有意义,以便每个人都是一个对象,例如:

[{name: "steve", age: 18, location: "Uk"}, {name: "conor", age: 18, location: "Uk"}]

回答by Jesse Fender

I know this is an older post but I have a pretty cool solution that gets key from the array and then uses that to return the first item, or alternatively last, the current code is es6 but its simple to convert to es5.

我知道这是一篇较旧的帖子,但我有一个非常酷的解决方案,它从数组中获取键,然后使用它返回第一个项目,或者最后一个,当前代码是 es6,但转换为 es5 很简单。

Array.prototype.first = function () { let k = Object.keys( this ); return this[ k[ 0 ] ]; }
//re for last...
Array.prototype.last = function () { let k = Object.keys( this ); return this[ k[ k.length - 1 ] ]; }

To change to es5 use var rather than let.

要更改为 es5,请使用 var 而不是 let。

Reason for this prototype is to make sure that regardless or the index system used will always return first element or the last element...

这个原型的原因是确保无论使用什么索引系统都将始终返回第一个元素或最后一个元素......

回答by skellertor

A new feature that javascript has is array destructuring!

javascript 的一个新特性是数组解构!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

It removes all the complexity of using a custom function, or using the hard coded 0index (seems like a code smell).

它消除了使用自定义函数或使用硬编码0索引(看起来像代码异味)的所有复杂性。

Just destructure the value into a variable. If the array is empty the value will be undefined.

只需将值解构为变量即可。如果数组为空,则值为undefined

const arr = ["one", "two", "three"];
const [first] = arr;