Last updated: Mar 4, 2024
Reading timeยท3 min
To get the last item in an object:
Object.keys()
method to get an array of the object's keys.Object.values()
method to get an array of the object's values.Array.pop()
method to get the last key and value in the object.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
The Object.keys() method returns an array of the object's keys.
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.
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.
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.
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.
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.
This is a two-step process:
Object.entries()
method to get an array of key-value pairs.Array.pop()
method to get the last key-value pair.const obj = {one: 1, two: 2, three: 3}; const [key, value] = Object.entries(obj).pop(); console.log(key); // ๐๏ธ three console.log(value); // ๐๏ธ 3
The Object.entries() method returns an array of the given object's key-value pairs.
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.
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()
.
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.
You can learn more about the related topics by checking out the following tutorials: