如何获取 JavaScript 对象中的最后一项?
如果我有一个像这样的对象:
{ 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' }
如果我事先不知道列表会上升到“c”,除了循环访问该对象之外,是否有办法获取对象中的最后一项(例如 '胡萝卜'
)?
If I have an object like:
{ 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' }
If I don't know in advance that the list goes up to 'c', other than looping through the object, is there a way to get the last item in the object (e.g. 'carrot'
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
是的,有一种方法使用
Object.keys(obj)
。 此页面对此进行了解释:如果您想要获取最后一个对象的值,你可以这样做:
Yes, there is a way using
Object.keys(obj)
. It is explained in this page:If you want to get the value of the last object, you could do this:
不会。 JSON 和大多数其他键值数据结构无法保证顺序,因此最后一项有时可能是
carrot
,有时可能是banana
等等。如果您需要依赖排序,那么最好的选择是使用数组。键值数据结构的强大之处在于通过其键
访问值,而不是能够获取对象的nth
项。No. Order is not guaranteed in JSON and most other key-value data structures, so therefore the last item could sometimes be
carrot
and at other times bebanana
and so on. If you need to rely on ordering, your best bet is to go with arrays. The power of key-value data structures lies in accessing values by theirkeys
, not in being able to get thenth
item of the object.其中 obj 是你的对象
where obj is your object
其他答案对我来说过于复杂。
The other answers overcomplicate it for me.
来源:http://javascriptweblog.wordpress.com< /a>
source:http://javascriptweblog.wordpress.com
使用ES6解构赋值语法的解决方案:
Solution using the destructuring assignment syntax of ES6:
你可以试试这个。这将存储最后一个项目。这里需要将obj转换为数组。然后使用数组
pop()
函数从转换后的数组中返回最后一项。You can try this. This will store last item. Here need to convert obj into array. Then use array
pop()
function that will return last item from converted array.至于Javascript中对象属性的顺序,我将链接到这个答案:
“for (... in ...)”循环中的元素顺序
具体来说:
所以这里的所有其他答案都是正确的,没有官方保证对象属性的顺序。然而在实践中是存在的(除非任何自然会搞砸官方指定行为的错误)。
此外,对象属性的实际枚举顺序可能会在未来的 EMCAScript 规范中进行编纂。
不过,目前我不会围绕此编写代码,主要是因为没有内置工具来帮助处理对象属性顺序。您可以编写自己的属性,但最终您总是会循环遍历对象中的每个属性以确定其位置。
因此,您的问题的答案是否,除了循环访问对象之外没有其他办法。
As for the ordering of object properties in Javascript, I will just link to this answer:
Elements order in a "for (… in …)" loop
Specifically:
So every other answer here is correct, there is no official guaranteed order to object properties. However in practice there is (barring any bugs which naturally can screw up even set-in-stone officially specified behavior).
Furthermore, the de-facto enumeration order of object properties is likely to be codified in future EMCAScript specs.
Still, at this time I would not write code around this, mostly because there are no built-in tools to help deal with object property order. You could write your own, but in the end you'd always be looping over each property in an object to determine its position.
As such the answer to your question is No, there is no way besides looping through an object.
如果顺序很重要,请使用数组,而不是对象文字。
或者类似的东西
,或者甚至..
dict
的键根本不保证是有序的。Use an array, not an object literal, if order matters.
Or something like
Or even..
The keys for
dict
are not guaranteed at all to be in order.您还可以使用
Object.values()
方法:Edit
要提高性能,您可以创建一个变量:
constfruitValues = Object.values(fruitObject);
给你:
fruitValues[fruitValues.length - 1];
You could also use the
Object.values()
method:Edit
To improve performance, you could create a variable:
const fruitValues = Object.values(fruitObject);
To give you:
fruitValues[fruitValues.length - 1];
看来路途很短。
https://developer.mozilla.org/de/文档/Web/JavaScript/Reference/Global_Objects/Object/values
https://developer.mozilla.org/de/文档/Web/JavaScript/Reference/Global_Objects/Array/slice
It seems to be a short way.
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/values
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
JavaScript 中的地图对象
。现在已经有3年左右的历史了。此地图数据结构保留项目插入的顺序。这样,检索最后一个项目实际上会导致最新的项目插入到地图中
Map object in JavaScript
. This is already about 3 years old now. This map data structure retains the order in which items are inserted. With this retrieving last item will actually result in latest item inserted in the Map
让
obj
成为您的对象。执行:Let
obj
be your object. Exec:如果您的意思是按字母顺序获取最后一个密钥,您可以(保证):
if you mean get the last key alphabetically, you can (garanteed) :