82

I have a simple object like the one below:

var countries = {
    "Argentina":1,
    "Canada":2,
    "Egypt":1,
};

I need to create two arrays. The first array is an array of all the keys from the object. I created this array by:

var labels = Object.keys(countries);

This works well. I obtain an array of countries. Now when I try to create an array from the values...

var labels = Object.values(countries);

I get this error: Uncaught TypeError: Object.values is not a function JavaScript

I don't know what I am doing wrong. I console.log countries before and after I declare labels and the object remains the same. How do I properly use Object.values()?

HoldOffHunger
  • 10,963
  • 6
  • 53
  • 100
Alex Fallenstedt
  • 1,668
  • 1
  • 13
  • 27
  • What browser are you using, because according to MDN it could not be [supported](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values#Browser_compatibility) – Mark C. Aug 03 '16 at 15:58
  • @MarkC. I am using Google Chrome 52.0.2743.82 – Alex Fallenstedt Aug 03 '16 at 16:00

7 Answers7

230

.values is unsupported in many browsers - you can use .map to get an array of all the values:

var vals = Object.keys(countries).map(function(key) {
    return countries[key];
});

See MDN doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values or Official doc: https://tc39.github.io/ecma262/#sec-object.values (thanks @evolutionxbox for correction)

tymeJV
  • 99,730
  • 13
  • 150
  • 152
  • 2
    Strange. `.values` seems so powerful. Thank you for showing me an alternative. It makes a lot more sense now! – Alex Fallenstedt Aug 03 '16 at 16:04
  • 1
    (psst, mdn whilst being amazing is not "official" documentation - https://tc39.github.io/ecma262/#sec-object.values) – evolutionxbox Aug 03 '16 at 16:33
  • What is not mention here is that Object.keys does rearrange returned array in array like objects with random key ordering so the return values may not be in the same order as in the original object. var anObj = { 100: 'a', 2: 'b', 7: 'c' }; console.log(Object.keys(anObj)); // console: ['2', '7', '100'] – user1502826 Dec 21 '17 at 02:41
  • 2
    Object keys are unordered anyways, so the array order shouldn't matter. – tymeJV Dec 21 '17 at 13:24
  • IE 11 is about the only modern browser not supporting `Object.values()`. Just bit us this morning. We'd tested on Chrome but not IE. Thanks, @tymeJV, for the great answer and example. – Alex Dec 18 '18 at 17:19
19

It's also worth noting that only Node versions >= 7.0.0 fully support this.

http://node.green

phobos
  • 502
  • 5
  • 12
16

For those who ended up here and are using Angular, adding import 'core-js/es7/object'; to polyfills.ts file solved the problem for me.

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/array';
import 'core-js/es6/date';
import 'core-js/es6/function';
import 'core-js/es6/map';
import 'core-js/es6/math';
import 'core-js/es6/number';
import 'core-js/es6/object';
import 'core-js/es6/parse-float';
import 'core-js/es6/parse-int';
import 'core-js/es6/regexp';
import 'core-js/es6/set';
import 'core-js/es6/string';
import 'core-js/es6/symbol';
import 'core-js/es6/weak-map';
import 'core-js/es7/array';
import 'core-js/es7/object'; // added import
j3ff
  • 4,539
  • 5
  • 35
  • 45
2

Looks like this issue is fixed in Safari latest version. I came around the same issue. This issue occurs in browser version 9.0.1 and does not occur in 10.1.1

Editing to add the attachments:

[snippet][1]
[object value][2]
[browser version][3]
HoldOffHunger
  • 10,963
  • 6
  • 53
  • 100
Venkata
  • 21
  • 4
  • 1
    Do you have a reference you could share for this information? A bug report or something? – Tim Hutchison Jun 27 '17 at 18:24
  • I donot have a reference. The mac systems carrying 9.0x versions are persistently causing the issue. But my system carrying 10.1.1 does not cause the same issue. – Venkata Jun 27 '17 at 19:14
2

Using "for...in" as discussed at mozilla: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

Here is the code I used:

function objectValues(obj) {
    let vals = [];
    for (const prop in obj) {
        vals.push(obj[prop]);
    }
    return vals;
}

// usage
let obj = {k1: 'v1', k2: 'v1', k3: 'v1'};

console.log(objectValues(obj));             // prints   the array  [ 'v1', 'v1', 'v1' ]
// OR
console.log(objectValues(obj).join(', '));  // prints   the string 'v1, v1, v1'
Manohar Reddy Poreddy
  • 16,412
  • 7
  • 111
  • 98
0

I think issue in compilation support on browsers compatibility, You can use map to achieve the same.

var countries = [
  {
"Argentina": 1,
"Canada": 2,
"Egypt": 1,
"india": 1
  },
  {
"Argentina": 1,
"india": 1,
"US": 2,
"UK": 1,

  }
];

var unpick = countries.map(d=>{ return Object.keys(d) });
console.log(unpick)

var countries = [
  {
"Argentina": 1,
"Canada": 2,
"Egypt": 1,
"india": 1
  },
  {
"Argentina": 1,
"india": 1,
"US": 2,
"UK": 1,

  }
];

var unpick = countries.map(d=>{ return Object.values(d) });
console.log(unpick)
KARTHIKEYAN.A
  • 11,752
  • 4
  • 81
  • 93
0

Consider using _.values(object)

Docs: https://lodash.com/docs/4.17.11#values

Matias Elorriaga
  • 7,467
  • 3
  • 34
  • 53