Last updated: Mar 3, 2024
Reading timeยท3 min
To get a value of an object by index:
Object.values()
method to get an array of the object's values.const obj = {country: 'Chile', name: 'bobby hadz'}; const firstValue = Object.values(obj)[0]; console.log(firstValue); // ๐๏ธ "Chile" const firstKey = Object.keys(obj)[0]; console.log(firstKey); // ๐๏ธ "country"
The Object.values() method returns an array of the object's values.
const obj = {country: 'Chile', name: 'bobby hadz'}; // ๐๏ธ [ 'Chile', 'bobby hadz' ] console.log(Object.values(obj));
The order of the array returned by the Object.values()
method is the same as
that provided in a for...in
loop.
JavaScript indexes are zero-based, so the first element in an array has an index
of 0
and the last element has an index of array.length - 1
.
If you try to access the array at an index that's out of bounds, you would get
an undefined
value back.
const obj = {country: 'Chile', name: 'bobby hadz'}; console.log(Object.values(obj)); // ๐๏ธ [ 'Chile', 'bobby hadz' ] console.log(Object.values(obj)[100]); // ๐๏ธ undefined
The object doesn't have a value at index 100
, so the expression returns
undefined
.
A more indirect approach is to use the Object.keys() method.
To get an object's key by index:
Object.keys()
method to get an array of the object's keys.const obj = {number: 5, color: 'blue'}; const secondKey = Object.keys(obj)[1]; console.log(secondKey); // ๐๏ธ "color"
We used the Object.keys() method to get an array of the object's keys.
const obj = {number: 5, color: 'blue'}; console.log(Object.keys(obj)); // ๐๏ธ ['number', 'color']
The only parameter the method takes is the object, for which to return the keys.
The ordering of the keys in the array is the same as provided by a for...in
loop.
The last step is to access the array of keys at the specified index.
0
and the last element has an index of array.length - 1
.If you try to get a key at an index that's out of bounds, you will get an
undefined
value back.
const obj = {number: 5, color: 'blue'}; console.log(Object.keys(obj)[100]); // ๐๏ธ undefined
If you need to get an object's value by index, you would use the
Object.values()
method.
const obj = {name: 'Alice', age: 30}; const firstValue = Object.values(obj)[0]; console.log(firstValue); // ๐๏ธ "Alice"
The Object.values() method returns an array of the object's values.
const obj = {name: 'Alice', age: 30}; // ๐ ['Alice', 30] console.log(Object.values(obj));
You can you bracket notation to access the value at a specific index, just like we did with the array of keys.
There is also an Object.entries() method that you can use to get an object's key-value pair by index.
const obj = {number: 5, color: 'blue'}; const entries = Object.entries(obj); const [key, value] = entries[0]; console.log(key); // ๐๏ธ number console.log(value); // ๐๏ธ 5
The Object.entries()
method returns an array of the object's key-value pairs.
const obj = {number: 5, color: 'blue'}; const entries = Object.entries(obj); // ๐๏ธ [ [ 'number', 5 ], [ 'color', 'blue' ] ] console.log(entries);
The first element in each nested array is the key and the second is the value.
This is a three-step process:
Object.keys()
method to get an array of the object's keys.const obj = {country: 'Chile', name: 'bobby hadz'}; const keys = Object.keys(obj); console.log(obj[keys[0]]); // ๐๏ธ Chile
The Object.keys()
method returns an array of the object's keys.
const obj = {country: 'Chile', name: 'bobby hadz'}; const keys = Object.keys(obj); console.log(keys); // ๐๏ธ [ 'country', 'name' ]
We have to get the key at the specified index and use it to get the corresponding value.
You can learn more about the related topics by checking out the following tutorials: