Question

I have this code in JS, which doesn't work in IE8.

Offices.forEach(function(trade) {

            console.log('Id for this trade is: '+trade.ID);

           });

How can I make it work?

Was it helpful?

Solution

foreach is not supported in IE8 (see documentation). Here's the shim:

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = 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 thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++)
    {
      if (i in t)
        fun.call(thisArg, t[i], i, t);
    }
  };
}

Or you could use a library like underscore which has its own cross-browser implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top