Monday, May 20, 2024
67
rated 0 times [  73] [ 6]  / answers: 1 / hits: 87625  / 7 Years ago, thu, march 16, 2017, 12:00:00

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?


More From » internet-explorer

 Answers
3

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])

[#58521] Tuesday, March 14, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carl

Total Points: 633
Total Questions: 105
Total Answers: 105

Location: Fiji
Member since Wed, Jul 14, 2021
3 Years ago
;