Mikarma
Mikarma

Reputation: 99

JavaScript Object.keys() method alternative for compatibility with IE8

I'm getting in error when testing in IE8 that the Object method is not supported. I'm using Object.keys()

Object.keys(jsoncont).sort(function(a,b){
   return b.localeCompare(a)
 }).forEach(function(key) {
    var val = jsoncont[key];

   /* My code here */
 });
}

Is there a good workaround for this method that is supported by IE8?

Upvotes: 6

Views: 20768

Answers (3)

naorz
naorz

Reputation: 341

The code below supports (by MDN docs) with all browsers.
Note: IE6+, I didn't test on IEs - it's just by docs.

You can validate a code compatibility by an online tool JS compatibility checker

function getKeys(obj) {
  var keys = [];
  iterate(obj, function (oVal, oKey) { keys.push(oKey) });
  return keys;
}

function iterate(iterable, callback) {
  for (var key in iterable) {
    if (key === 'length' || key === 'prototype' || !Object.prototype.hasOwnProperty.call(iterable, key)) continue;
    callback(iterable[key], key, iterable);
  }
}

Here are the browser APIs I just used by compatibility:

  1. for...in All browsers (IE6+)
  2. hasOwnProperty All browsers
  3. Function.prototype.call All browsers
  4. continue All browsers
  5. Array.prototype.push All browsers (IE 5.5 +)

Summary: IE 6+ support

iterate function can be used for both objects and arrays as well.

Upvotes: 4

Daab
Daab

Reputation: 101

Mozilla has an explanation of how to polyfill the function in older browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

// 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;
    };
  }());
}

Upvotes: 10

thefourtheye
thefourtheye

Reputation: 239483

If jsoncont is an object, you can use for...in

for (var key in jsoncont) {
...
}

Or as suggested in this blog post, you can create it like this

if (!Object.keys) Object.keys = function(o) {
  if (o !== Object(o))
    throw new TypeError('Object.keys called on a non-object');
  var k=[],p;
  for (p in o) if (Object.prototype.hasOwnProperty.call(o,p)) k.push(p);
  return k;
}

Upvotes: 7

Related Questions