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

Asked

Viewed 663 times

4

Guys, I got a problem. In a javascript file that we use works normally the Object.values() method that takes an object and returns an array. But on the internet explore 9 does not work. Someone could help?

example of code:

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

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

3 answers

2

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

inserir a descrição da imagem aqui

For more compatibility, 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]

1


This method does not work in some browsers.

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

1

Object values. is a new Javascript method not yet implemented in all browsers. It therefore did not exist 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.