Languages

JavaScript - Uncaught TypeError: Object.values is not a function

0 points
Asked by:
Vanessa-Drake
718

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
Answered by:
Vanessa-Drake
718

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

  1. Object.values() - JavaScript | MDN
  2. Array.prototype.map() - JavaScript | MDN
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join