1

javascript code shows error in Internet Explorer but not in FF.

$(document).ready(function(){




var countries = [
  ["Afghanistan","Af"],
  ["Åland Islands","Ax"],
  ["Zimbabwe","Zw"]
];

 var countryNames = countries.map(function(country){
  return {
    /*label: '<div class="flag '+country[1].toLowerCase()+'">'+country[0]+'</div>',*/
    value: country[0]
  }

  var my_var=countryNames();
}); 

});

In IE 8 developer tool, if I debug js, the error message is :' Object doesn't support this property or method'. And the error is indicated at the snippet starting with the line :

var countryNames = countries.map(function(country){

How to make the code work in IE ?

2
  • 2
    Which version of Internet Explorer are you using? According to the MDN entry for the Array map() function, it's only supported in IE9. Commented Sep 19, 2012 at 9:05
  • IE8, I need to make the code work for all IE except IE6 Commented Sep 19, 2012 at 9:15

4 Answers 4

4

The Array.prototype.map() function is only supported in Internet Explorer 9, so that code won't work in earlier versions of the browser. Since you've tagged the question as jQuery, you could use the jQuery.map() function instead:

var countryNames = jQuery.map(countries, function(country) {
    return { 
        value: country[0]
    }
});

jsFiddle DEMO tested using Internet Explorer 9 in IE7 mode.

Sign up to request clarification or add additional context in comments.

Comments

1

Use Jquery .each() instead http://api.jquery.com/jQuery.each/

var countryNames = [];    
$.each(countries, function(index, value){
    countryNames.push(value[0]);
}

1 Comment

If they're going down the jQuery route, why not use jQuery.map()?
0

Not all versions (if any) have Array.prototype.map

This is a polyfill/MonkeyPatch you can use to support it under IE -- but including this code may cause problems if you use for ... in on your arrays.

Array.prototype.map

Comments

0
  var my_var=countryNames();
}); 

should be:

}); 
  var my_var=countryNames;

Worked in IE9 for me.

Or you can you use $.each like Neil Kennedy said.

1 Comment

The countryNames variable references an array, not a function, so that line can be removed entirely because it just throws an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.