What to use instead of Object.keys ()?

I need to find something in jQuery that can work in IE8 as well as in real browsers. I am new to jQuery, here 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 fine in Chrome, IE10 and FF, what can I do for IE8?

+6
source share
1 answer

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 also includes other gaskets):

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

+10
source

Source: https://habr.com/ru/post/950114/


All Articles