Method Object.values () does not work in internet explorer 9

4

Personally I have a problem. In a javascript file we use the Object.values () method that normally takes an object and returns an array. But on the internet explore 9 does not work. Could someone help?

code example:

var json = '{"6":{"dataInicio":"02\/01\/2017","dataFim":"08\/02\/2017","bonus":"10","idProd":6}}';
var obj = JSON.parse(json);
var array = Object.values(obj);

No ie shows the following error: SCRIPT438: Object does not support property or method 'values'

    
asked by anonymous 17.03.2017 / 12:29

3 answers

1

This method does not work in some browsers.

Try: var array = Object.keys(obj).map(function(key){ return obj[key];});

    
17.03.2017 / 12:36
2

Object.values is not supported by Internet Explorer , as you can see in this table:

Forgreatercompatibility,use for..in :

var obj = {a:1, b:2, c:3};
var array = [];

for (var propriedade in obj) {
  array.push(obj[propriedade ]);
}
console.log(array); // [1,2,3]
    
17.03.2017 / 12:38
1

Object.values is a new method in JavaScript not yet implemented in all browsers. It did not exist then at the time of IE9.

But you can use a polyfill like this:

if (!Object.values) {
  Object.prototype.values = function(object) {
    var values = [];
    var keys = Object.keys(object);
    for (var i = 0; i < keys.length; i++) {
      var k = keys[i];
      values.push(object[k]);
    }
    return values;
  }
}

var values = Object.values({
  foo: 'bar',
  fuji: 'baz'
});
console.log(values);
    
17.03.2017 / 12:41