To retrieve a property of a JSON object by index with JavaScript, we use the Object.values
method.
For instance, we write
const obj = {
set1: [1, 2, 3],
set2: [4, 5, 6, 7, 8],
set3: [9, 10, 11, 12],
};
const values = Object.values(obj);
console.log(values[1]);
to call Object.values
with obj
to return an array of obj
property values.
Then we get the 2nd entry from values
with values[1]
.
Conclusion
To retrieve a property of a JSON object by index with JavaScript, we use the Object.values
method.