Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.keys not working in internet Explorer

I have a program to return a list of keys from dictionary. The Code works correctly in Chrome, Opera and Firefox but not Internet Explorer. I have added alert comments to close down where the issue is. Below is the code causing the problem. The alerts are shown in the order

  • App Initializing
  • Getting JSON
  • Got JSON
  • Got Keys (Does not show in IE)

I found a similar Question here but I believe in this example that this isn't the correct question as I created the dictionary so it is a native object.

I am no longer sure that Object.keys is the problem so here is a link to the full page. I JavaScript is in page to make it easier to view

http://www.londonlayout.co.uk/dev/live.htm

 var myApp = {     init: function () {         var def = $.Deferred();         alert('App Initializing');         $.getJSON('data/data.json', function (raw) {             alert('Getting JSON');             myApp.data = raw;             $.each(myApp.data, function (code, details) {                 try {                     myApp.nameDict[details.name] = code;                 }                 catch (e) {}             });             alert('Got JSON');             myApp.names = Object.keys(myApp.nameDict);             alert('Got Keys')             def.resolve();         });         return def.promise();     },     data: {},     nameDict: {} } 
like image 734
Peter Saxton Avatar asked Sep 20 '13 09:09

Peter Saxton


People also ask

How do I find the property key of an object?

To get object property name with JavaScript, we use the Object. keys method. const result = Object. keys(myVar); console.


2 Answers

Object.keys is not avaiable in IE < 9. As a simple workaround you could use:

if (!Object.keys) {   Object.keys = function(obj) {     var keys = [];      for (var i in obj) {       if (obj.hasOwnProperty(i)) {         keys.push(i);       }     }      return keys;   }; } 

Here is a more comprehensive polyfill:

// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys if (!Object.keys) {   Object.keys = (function () {     'use strict';     var hasOwnProperty = Object.prototype.hasOwnProperty,         hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),         dontEnums = [           'toString',           'toLocaleString',           'valueOf',           'hasOwnProperty',           'isPrototypeOf',           'propertyIsEnumerable',           'constructor'         ],         dontEnumsLength = dontEnums.length;      return function (obj) {       if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {         throw new TypeError('Object.keys called on non-object');       }        var result = [], prop, i;        for (prop in obj) {         if (hasOwnProperty.call(obj, prop)) {           result.push(prop);         }       }        if (hasDontEnumBug) {         for (i = 0; i < dontEnumsLength; i++) {           if (hasOwnProperty.call(obj, dontEnums[i])) {             result.push(dontEnums[i]);           }         }       }       return result;     };   }()); } 
like image 55
jabclab Avatar answered Oct 14 '22 08:10

jabclab


Alternatively if you have access to lodash you can use keys. e.g.

_.keys(yourObj);

like image 34
dalcam Avatar answered Oct 14 '22 08:10

dalcam