0

i'm using forEach which of course IE doesn't like. I have added the polyfill which is provided from Mozilla, however this only gets binded to the Array prototype.

So IE is still complaining as Object type doesn't suuport forEach

SCRIPT438: Object doesn't support property or method 'forEach'

Should I not be using forEach on an Object? Which works fine on all other browsers!

An example of how i'm using forEach:

var elements = {
    'test1': 'hello',
    'test2': 'world'
};
elements.forEach(function(element, i) {

    console.log(element);

}
Knocktorius Maxima
  • 371
  • 2
  • 6
  • 20
Martyn Ball
  • 4,679
  • 8
  • 56
  • 126
  • 1
    If you want to use forEach on an object, you can either iterate over it using `Object.keys(elements).forEach(k => { elements[k] })` or using lodash or a similar library – gillyhl Oct 17 '18 at 11:01
  • 3
    `forEach is` for arrays. Not for objects - https://i.imgur.com/JR6LNYq.jpg – Royi Namir Oct 17 '18 at 11:01
  • Object does not have the forEach – Pengcheng Oct 17 '18 at 11:01
  • You mean you stll have to support IE<9 ..? @gillyhl That would need a polyfill for the arrow function ... – Teemu Oct 17 '18 at 11:01
  • @Teemu oh yeah, damn IE – gillyhl Oct 17 '18 at 11:11
  • 1
    "_Which works fine on all other browsers!_" Definitely that won't work in any browser. Are you mixing JS objects and the hosted [NodeList](https://developer.mozilla.org/en-US/docs/Web/API/NodeList) objects? The latter has `forEach` method in the modern browsers, but not in IEs. – Teemu Oct 17 '18 at 12:06
  • 1
    @Teemu I was doing a forEach on a NodeList, this could of been my confusion as to why it was working on an Object! This all makes sense now! – Martyn Ball Oct 18 '18 at 11:45

0 Answers0