Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Object.keys() alternative for IE8

    I am using Object.keys() but apparently it isn't supported by IE8<= so I need an alternative. Here is the code I'm using now...

    Code:
    /* For compatibility with <=IE8 */
    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;
    }
    						
    Object.keys(jsoncont)
    .sort(function(a,b) {
    	return b.localeCompare(a)
    })
    for(key in jsoncont) {
    	var val = jsoncont[key];
    								
    	$('#contactSearchResultssearching').hide();
    	$('#contactSearchResults').append('<div class="resultset" id="radioset"><input type="radio"  id="' + val.id +'" name="customer_c_id" value="' + val.id + '" /></div><div class="resultset" id="nameset">' + val.first + '</div><div class="resultset" id="nameset">' + val.last + '</div><div class="resultset" id="emailset">' + val.email + '</div><div class="clearall"></div>');
    					
    };
    As you can see I tried to implement a function for browsers that don't support Object.keys() but it's not working in IE8. Can someone help me come up with a better solution to loop through the objects in an array in an object?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Code:
    if(!('keys' in Object)) Object.keys= function(o){
    	if(typeof o!= 'object' || o===null){
    		throw new TypeError('Object.keys called on a non-object or null');
    	}
    	var k= [];
    	for(var p in o) if(o.hasOwnProperty(p)) k.push(p); 
    	return k;
    }
    Last edited by mrhoo; 12-20-2013 at 05:45 PM.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    consider a couple improvements:

    Code:
    if(!Object.hasOwnProperty("keys")) Object.keys= function(o){
    	if(Object(o) !== o){
    		throw new TypeError('Object.keys called on a non-enumerable');
    	}
    	var k= [], p;
    	for(p in o){ if(o.hasOwnProperty(p)) k[k.length]=p; }
    	return k;
    };
    1. correctly handles a broader range of enumerable objects
    2. avoids Object.prototype.keys conflict
    3. considerably improves IE's array build performance using [].length instead of push
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •