Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use instead of Object.keys()?

Tags:

jquery

I need to find something in Jquery that can work in IE8 as well as real browsers. I'm brand new to Jquery, the following is my code that works in modern browsers:

$('#FIELD_'+office_id).on('change',function(){
    offices = $(this).val();
for(var i=0; i<=Object.keys(southland.address).length;i++){
        if(offices == Object.keys(southland.address)[i]){
            address = southland.address[offices]Object.keys(southland.address[offices])[0]];
        }
    }

where southland.address comes from an external array. This works perfect in Chrome, IE10 and FF, anything I can do for IE8?

like image 763
pedrum golriz Avatar asked Jul 23 '13 18:07

pedrum golriz


People also ask

Can I use object entries ()?

The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value] pairs. This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well.

Can I use number as object key?

Against what many think, JavaScript object keys cannot be Number, Boolean, Null, or Undefined type values. Object keys can only be strings, and even though a developer can use other data types to set an object key, JavaScript automatically converts keys to a string a value.

Why do we use object keys?

keys() Method: The Object. keys() method is used to return an array whose elements are strings corresponding to the enumerable properties found directly upon an object. The ordering of the properties is the same as that given by the object manually in a loop is applied to the properties.

What is the difference between object key and object key?

key and object[key] accomplish the same thing. However, object. key only works if the key name is hardwired ( I mean not happening dynamically since it cannot change at run-time). It also does not work when the key is a number instead of a string.


1 Answers

To support Object.keys in older browsers, you can use this snippet:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Compatibility

if (!Object.keys) {
  Object.keys = (function () {
    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 = [];

      for (var prop in obj) {
        if (hasOwnProperty.call(obj, prop)) result.push(prop);
      }

      if (hasDontEnumBug) {
        for (var i=0; i < dontEnumsLength; i++) {
          if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
        }
      }
      return result;
    };
  })();
}

or this polyfill (which includes other shims, as well):

https://github.com/kriskowal/es5-shim/

like image 200
M Sost Avatar answered Oct 13 '22 12:10

M Sost