How to get the Last item in an Object using JavaScript

avatar
Borislav Hadzhiev

Last updated: Jan 8, 2023
3 min

banner

# Get the Last item in an Object in JavaScript

To get the last item in an object:

  1. Use the Object.keys() method to get an array of the object's keys.
  2. Use the Object.values() method to get an array of the object's values.
  3. Use the Array.pop() method to get the last key and value in the object.
index.js
const obj = {one: 1, two: 2, three: 3}; const lastKey = Object.keys(obj).pop(); console.log(lastKey); // ๐Ÿ‘‰๏ธ three const lastValue = Object.values(obj).pop(); console.log(lastValue); // ๐Ÿ‘‰๏ธ 3

get last item in object

The Object.keys method returns an array of the object's keys.

index.js
const obj = {one: 1, two: 2, three: 3}; // ๐Ÿ‘‡๏ธ ['one', 'two', 'three'] console.log(Object.keys(obj));

The Object.values() method returns an array of the object's values.

index.js
const obj = {one: 1, two: 2, three: 3}; console.log(Object.values(obj)); // ๐Ÿ‘‰๏ธ [1, 2, 3]

The last step is to use the Array.pop() method to remove and return the last element from the arrays.

index.js
const obj = {one: 1, two: 2, three: 3}; const lastKey = Object.keys(obj).pop(); console.log(lastKey); // ๐Ÿ‘‰๏ธ three const lastValue = Object.values(obj).pop(); console.log(lastValue); // ๐Ÿ‘‰๏ธ 3

You can also get the value by using the key.

index.js
const obj = {one: 1, two: 2, three: 3}; const lastKey = Object.keys(obj).pop(); console.log(lastKey); // ๐Ÿ‘‰๏ธ three const v = obj[lastKey]; console.log(v); // ๐Ÿ‘‰๏ธ 3

We used bracket notation to access the value that's associated with the property.

If you don't want to use the Array.pop() method, you could also use the Array.at() method.

index.js
const obj = {one: 1, two: 2, three: 3}; const lastKey = Object.keys(obj).at(-1); console.log(lastKey); // ๐Ÿ‘‰๏ธ three const lastValue = Object.values(obj).at(-1); console.log(lastValue); // ๐Ÿ‘‰๏ธ 3

The Array.at() method returns the array element at the specified index.

The method supports negative indices to count backward, where -1 is the last array element, -2 the second to last, etc.

# Get the last key-value pair in an object using Object.entries()

This is a two-step process:

  1. Use the Object.entries() method to get an array of key-value pairs.
  2. Use the Array.pop() method to get the last key-value pair.
index.js
const obj = {one: 1, two: 2, three: 3}; const [key, value] = Object.entries(obj).pop(); console.log(key); // ๐Ÿ‘‰๏ธ three console.log(value); // ๐Ÿ‘‰๏ธ 3

get last key value pair in object using object entries

The Object.entries method returns an array of the given object's key-value pairs.

index.js
const obj = {one: 1, two: 2, three: 3}; // ๐Ÿ‘‡๏ธ [['one', 1], ['two', 2], ['three', 3]] console.log(Object.entries(obj));

We used the Array.pop() method to get the last key-value pair array.

The last step is to use destructuring assignment to assign the first and second array elements to the key and value variables.

index.js
const [key, value] = ['three', 3]; console.log(key); // ๐Ÿ‘‰๏ธ three console.log(value); // ๐Ÿ‘‰๏ธ 3

An easy way to think about the destructuring assignment is that we're assigning the array elements to variables with the order of assignment being preserved.

You can also use the Array.at() method instead of Array.pop().

index.js
const obj = {one: 1, two: 2, three: 3}; const [key, value] = Object.entries(obj).at(-1); console.log(key); // ๐Ÿ‘‰๏ธ three console.log(value); // ๐Ÿ‘‰๏ธ 3

The Array.at method takes an integer and returns the item at that index.

The method allows for positive and negative integers.

You can use negative integers to count back from the end of the array.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev