4

I have below snippet

data.forEach(function (row) {
var dataRow = [];

columns.forEach(function (column) {

dataRow.push(row[column].toString());
})

which is giving me error data.forEach(function (row) { .What should be alternate to this? How to resolve it?

RAM
  • 75
  • 2
  • 10
  • 1
    Else you can also use the polyfil https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – brk Sep 11 '18 at 04:56
  • 1
    @brk this polyfill is for Array#forEach The one the dupe target was talking about is [NodeList#forEach](https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach#Polyfill). Also, RAM can you confirm it was indeed this NodeList#forEach that you were trying to use. – Kaiido Sep 11 '18 at 05:04
  • [nodelist#forEach polyfill](https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach#Polyfill) - I doubt the OP was talking about NodeList though – Jaromanda X Sep 11 '18 at 05:14
  • Yes, it sounds more like `data` is not what it should be. I reopen ([save-link-to-prev-dupe-target](https://stackoverflow.com/questions/47534102/js-foreach-loops-in-ie11)). But note that as it stands this question should still be closed, because it lacks some information and debugging steps you should have taken on your side (like what is `data` before you call its non-existent forEach method?) – Kaiido Sep 11 '18 at 05:20
  • You can't loop through forEach when data is an object. It should run on array. In order to loop through the object, you should try: for (var key in data) { if (data.hasOwnProperty(key)) { console.log(key + " -> " + data[key]); // Here you will get key and value } } – Ankit Sep 11 '18 at 05:50

2 Answers2

12

For anyone using document.querySelectorAll('..').forEach() and having this issue in IE 11 saying "forEach is not a function", I found a hack on reddit which worked nicely:

if (typeof NodeList.prototype.forEach !== 'function')  {
    NodeList.prototype.forEach = Array.prototype.forEach;
}

This works perfectly and is 3 lines of code instead of a polyfill.

@JoeTaras hinted at this in his answer (yes IE does have Array.forEach since IE9), but I still think my answer adds value and will help other users.

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
  • 1
    This was posted at the perfect time! Many thanks! Don't know why the damage, but this is gold! – klewis Oct 14 '20 at 00:52
2

IE11 knows forEach statement (is IE compliant from IE 9.0, see here), but if you want you can use instead of forEach you can use for statement, as follow:

I've edited my answer, add a check on data object if is an array

if (data != null && Array.isArray(data)) {
    var dataRow = [];
    for (var i = 0; i < data.length; i++) {
        var row = data[i];
        for (var j = 0; j < columns.length; i++) {
            var column = columns[i];

            dataRow.push(row[column].toString());
        }
    }
}
Joe Taras
  • 15,166
  • 7
  • 42
  • 55
  • That's assuming the `data` variable holds an indexed object, which is a strong assumption given it doesn't have a *#forEach* method. + `var foo=bar` in for loop... – Kaiido Sep 11 '18 at 06:37
  • @Kaiido: Hi, yes, I'm based on question. But now I edit my question – Joe Taras Sep 11 '18 at 06:38
  • 1
    Ah no, we know at least that `data` **is not** an Array. Luckily IE11 does support Array#forEach – Kaiido Sep 11 '18 at 06:40