Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative version for Object.values()

I'm looking for an alternative version for the Object.values() function.
As described here the function is not supported in Internet Explorer.

When executing the following example code:

var obj = { foo: 'bar', baz: 42 }; console.log(Object.values(obj)); // ['bar', 42] 

It works in both, Firefox and Chrome, but throws the following error in IE11:

Object doesn't support property or method "values"

Here you can test it: Fiddle.

So, what would be a quick fix?

like image 954
Evgenij Reznik Avatar asked Mar 16 '17 09:03

Evgenij Reznik


People also ask

How do you change the value of an object?

To change the value of an existing property of an object, specify the object name followed by: a dot, the name of the property you wish to change, an equals sign, and the new value you wish to assign.

What is an object value?

In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object. Examples of value objects are objects representing an amount of money or a date range.

What does .value do in JavaScript?

Object.values() Method Object. values() takes the object as an argument of which the enumerable own property values are to be returned and returns an array containing all the enumerable property values of the given object.


1 Answers

You can get array of keys with Object.keys() and then use map() to get values.

var obj = { foo: 'bar', baz: 42 };  var values = Object.keys(obj).map(function(e) {    return obj[e]  })    console.log(values)

With ES6 you can write this in one line using arrow-functions.

var values = Object.keys(obj).map(e => obj[e]) 
like image 157
Nenad Vracar Avatar answered Sep 21 '22 13:09

Nenad Vracar