1

I have the following javascript -

var trim = intsAsJSArray.replace(/[\[\]]/g, "");
       var data = trim.split(",");

       data = data.map(function (d) {
           return +d;
       });

However this is failing in IE with the error -

Error: Object doesn't support property or method 'map'

This is confusing as it works in both Chrome and FireFox. Why is this?

6
  • 1
    map is available in IEs > 8. If you're using newer version, check the DTD is properly declared. Commented Apr 1, 2014 at 15:01
  • 3
    data = $.map(data, function(d) { return +d; }); Commented Apr 1, 2014 at 15:02
  • Already answered: stackoverflow.com/questions/18500942/… Commented Apr 1, 2014 at 15:03
  • @Jack I think you'd need a .get() at the end of that, right? Or am I thinking of something else? Commented Apr 1, 2014 at 15:08
  • 1
    @Ian: You're thinking of jQuery#map, not jQuery.map. (How's that for robust API design? ;-) ) Commented Apr 1, 2014 at 15:11

3 Answers 3

7

IE8 doesn't support most of ES5 Array methods - including Array.map. So you'll have to either use a ES5 shim that extends Array.prototype, or switch to a library that has ES5 methods attached to their own objects - like jQuery ($.map) and Underscore (_.map).

As you marked your question with jquery tag, there's a big chance you already use one, so it's enough to change your code like this:

var data = $.map(trim.split(","), Number);
Sign up to request clarification or add additional context in comments.

Comments

2

Array#map was added in ES5. IE8 came out before the ES5 specification was completed, and doesn't include most ES5 features (including map). IE9 has Array#map and most (but not all) other ES5 additions; IE10+ has the complete set of ES5 features. Here's a rundown of ES5 support in various browsers.

If you search for "es5 shim," you'll find various options for adding in the things that can be retroactively added to older environments (which includes Array#map).

Since you've tagged your question jquery, you can use the $.map function jQuery supplies.

Comments

0

IE8 doesn't support map because it has been added in ES5.

In case you want to add a shim for that have a look at this polyfil from the MDN website:

if (!Array.prototype.map) {

    Array.prototype.map = function (fun /*, thisArg */ ) {
        "use strict";

        if (this === void 0 || this === null)
            throw new TypeError();

        var t = Object(this);
        var len = t.length >>> 0;
        if (typeof fun !== "function")
            throw new TypeError();

        var res = new Array(len);
        var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
        for (var i = 0; i < len; i++) {
            // NOTE: Absolute correctness would demand Object.defineProperty
            //       be used.  But this method is fairly new, and failure is
            //       possible only if Object.prototype or Array.prototype
            //       has a property |i| (very unlikely), so use a less-correct
            //       but more portable alternative.
            if (i in t)
                res[i] = fun.call(thisArg, t[i], i, t);
        }

        return res;
    };
}

Otherwise just write your own function to create a new object with the values you want of the input.

1 Comment

LOL I love the "use strict" -- I'm reasonably certain there's never been any JavaScript engine that didn't have Array#map but did have strict mode.

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.