Skip to content Skip to sidebar Skip to footer

The Easiest Way To Access An Object's Single Own Property?

I have an object which will only have one property (own property). What is the easiest way to access that property's value? Something like: value = obj[]; I

Solution 1:

something like

var value = obj[ Object.keys(obj)[0] ];

getting the keys with Object.keys and the first (and only) key with [0]

Solution 2:

This should work.

var keys = Object.keys(obj);
var value = obj[keys[0]];

We can make it shorter

var value = obj[Object.keys(obj)[0]];

Post a Comment for "The Easiest Way To Access An Object's Single Own Property?"