Support for Object.values()
, Object.entries()
and Object.getOwnPropertyDescriptors()
has landed in the Google Chrome dev channel. These newly added methods are currently candidate proposals and at stage 3 for the ES2017 specification. Many of us were using Lodash for some of the aforementioned missing features from browsers like Object.values. So these are welcome additions.
Keep in mind that these have not landed in the consumer release version of Google Chrome, maybe in a month or two we will see these released, perhaps even sooner. For the moment they are hidden behind a flag and only in the Canary version, enabled in options under “Experimental Javascript”. I did not realise these additions were so far along in the specification and hopefully provided there are no delays, we see them land in the finalised spec later next year.
If you are not aware of what any of these methods are for, here is a brief explainer for you.
Object.values
Returns an array of an objects enumerable property values. So if you had an object with the following values: var obj = {prop: 'me', prop2: 'far', prop3: 'so', prop4: 'la'}
and ran that through Object.values(obj)
, you would get the following: ['me', 'far', 'so', 'la']
Object.entries
Returns an array of an objects enumerably keys and values in pairs [key, value]. Taking the previous object example: var obj = {prop: 'me', prop2: 'far', prop3: 'so', prop4: 'la'}
and running that through Object.entries(obj)
you would get the following: [ ['prop', 'me'], ['prop2', 'far'], ['prop3', 'so'], ['prop4', 'la'] ]
. This is a game changer for many reasons, but it allows us to iterate over the properties of an object.
Object.getOwnPropertyDescriptors
This one is a mouthful to type, huh? This method returns an object that is comprised of each own non-inherited property of the supplied object, it adds a property to the returned object with a matching key and value which is the former properties descriptor.
The exciting thing about this addition is that it will allow us to copy properties into an object without abusing Object.assign and most exciting of all, we can use it to clone objects. You could clone an object using code like this: let cloned = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
– this creates a new object, takes the prototype value of our supplied object and gets the keys/values using the getOwnPropertyDescriptors
method.