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
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: {} } To get object property name with JavaScript, we use the Object. keys method. const result = Object. keys(myVar); console.
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;     };   }()); } Alternatively if you have access to lodash you can use keys. e.g.
_.keys(yourObj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With