EN
JavaScript - Uncaught TypeError: Object.values is not a function
1
answers
0
points
Every time I try to create an array from the object values I get the following error:
Uncaught TypeError: Object.values is not a function
How can I fix that?
My code:
var letters = {
A: 1,
B: 2,
C: 3,
};
var values = Object.values(letters); // doesn't work, expected result: [ 1, 2, 3 ]
1 answer
0
points
The values()
method is not supported in many browsers (or some older versions).
You can use map()
method to get an array of values.
Practical example:
// ONLINE-RUNNER:browser;
var letters = {
A: 1,
B: 2,
C: 3,
};
var values = Object.keys(letters).map(function(key) {
return letters[key];
});
console.log(values); // [ 1, 2, 3 ]
References
0 comments
Add comment