Getting last element in javascript object

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', except for a loop through the object, is there a way to get the last element in the object (for example, 'carrot' )?

+80
javascript object
Nov 30 '10 at 19:05
source share
11 answers

No. An order is not guaranteed in JSON and most other data structures with key values, so the last element can sometimes be carrot , and in other cases, banana , etc. If you need to rely on ordering, it is best to go with arrays. The power of key value data structures is in accessing the values ​​of their keys , and not in being able to get the nth element of an object.

+58
Nov 30 '10 at 19:08
source share

Yes, there is a way to Object.keys(obj) . This is explained in this page :

 var fruitObject = { 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' }; Object.keys(fruitObject); // this returns all properties in an array ["a", "b", "c"] 

If you want to get the value of the last object, you can do this:

 fruitObject[Object.keys(fruitObject)[Object.keys(fruitObject).length - 1]] // "carrot" 
+190
May 16 '13 at 14:32
source share
 last = Object.keys(obj)[Object.keys(obj).length-1]; 

where obj is your object

+18
Feb 07 '14 at 7:58
source share
 var myObj = {a: 1, b: 2, c: 3}, lastProperty; for (lastProperty in myObj); lastProperty; //"c"; 

source: http://javascriptweblog.wordpress.com

+12
Aug 31 '12 at 19:44
source share

Solution Using ES6 Destructuring Destination Syntax:

 var temp = { 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' }; var { [Object.keys(temp).pop()]: lastItem } = temp; console.info(lastItem); //"carrot" 
+7
Sep 28 '17 at 12:44 on
source share

Use an array, not an object literal, if order matters.

 list = ['apple', 'banana', 'carrot']; 

Or something like

 dict = { 'a' : ['apple', 'awesome'], 'b' : ['best friend'] }; 

Or even..

 dict = [{letter:'a', list:['apple', 'awesome']},{letter:'b', list:['best friend']}]; 

The keys for dict not guaranteed in order.

+5
Nov 30 '10 at 19:10
source share

Regarding the ordering of the properties of an object in Javascript, I will simply contact this answer:

The order of elements in the "for (... in ...)" loop

In particular:

All modern implementations of ECMAScript iterate through a property object in the order in which they were defined

Thus, every other answer is correct here, there is no official guaranteed order for object properties. However, in practice there is (a ban on any errors that, of course, can spoil even officially established behavior).

In addition, the de facto order of listing object properties is likely to be codified in future EMCAScript specifications.

However, at this time I will not write code around this, mainly because there are no built-in tools to help deal with the order of object properties. You can write your own, but at the end you will always iterate over each property in the object to determine its position.

So the answer to your question is No , there is no way other than scrolling through an object.

+5
Nov 30 '10 at 19:34
source share
 JSArray = { 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' }; document.write(Object.keys(JSArray)[Object.keys(JSArray).length-1]);// writes 'c' document.write(JSArray[Object.keys(JSArray)[Object.keys(JSArray).length-1]]); // writes 'carrot' 
+3
Mar 06 '14 at 20:49
source share

You can also use the Object.values() method:

 Object.values(fruitObject)[Object.values(fruitObject).length - 1]; // "carrot" 
+1
Aug 10 '18 at 11:06
source share

A map object in JavaScript . It's been around three years now. This map data structure preserves the insertion order of elements. With this extraction of the last element, in fact, the last element inserted into the Map will be obtained

0
Feb 11 '16 at 1:15
source share

Let obj be your object. Exec:

 (_ => _[Object.keys(_).pop()])( obj ) 
0
Dec 05 '18 at 22:36
source share



All Articles