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'
)?
This question is related to
javascript
object
Yes, there is a way using Object.keys(obj)
. It 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 could do this:
fruitObject[Object.keys(fruitObject)[Object.keys(fruitObject).length - 1]] // "carrot"
var myObj = {a: 1, b: 2, c: 3}, lastProperty;
for (lastProperty in myObj);
lastProperty;
//"c";
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
are not guaranteed at all to be in order.
Solution using the destructuring assignment syntax of ES6:
var temp = { 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' };_x000D_
var { [Object.keys(temp).pop()]: lastItem } = temp;_x000D_
console.info(lastItem); //"carrot"
_x000D_
last = Object.keys(obj)[Object.keys(obj).length-1];
where obj is your object
You could also use the Object.values()
method:
Object.values(fruitObject)[Object.values(fruitObject).length - 1]; // "carrot"
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
The other answers overcomplicate it for me.
let animals = {
a: 'dog',
b: 'cat',
c: 'bird'
}
let lastKey = Object.keys(animals).pop()
let lastValue = animals[Object.keys(animals).pop()]
if you mean get the last key alphabetically, you can (garanteed) :
var obj = { 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' };
var keys = Object.keys(obj);
keys.sort();
var lastkey = keys.pop() // c
var lastvalue = obj[lastkey] // 'carrot'
Let obj
be your object. Exec:
(_ => _[Object.keys(_).pop()])( obj )
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'
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.
var obj = { 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' };_x000D_
var last = Object.keys(obj).pop();_x000D_
console.log(last);_x000D_
console.log(obj[last]);
_x000D_
As for the ordering of object properties in Javascript, I will just link to this answer:
Elements order in a "for (… in …)" loop
Specifically:
All modern implementations of ECMAScript iterate through object properties in the order in which they were defined
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.
Source: Stackoverflow.com