0

I have an array of objects:

var conversions = [
    { regex: ..., names: [ ... ] },
    ...
];

I loop through each conversion object and pass them to a certain function:

conversions.forEach(function(conv) {
    // function selection logic

    var result = func(message, conv); // func is the selected function, message is defined before

    // result logic
}

The loop works fine for all the objects in the array, but after the last object, a weird object is passed to the callback which is not part of my array. Adding console.log(conv); at the very beginning of the callback prints out all the conversions correctly, but the last weird object is printed out as epäluku undefined. "Epäluku" is a bad Finnish translation for NaN, which means the output is the same as NaN.toLocaleString() + " " + undefined.

I haven't been able to figure out why this odd object is there, or how to filter it. I've tried checking it against null and undefined but neither did the trick. I've even tried checking it against NaN.toLocaleString() + " " + undefined but that didn't work either.

This is part of a userscript, running in Chrome using Tampermonkey.

Spans
  • 364
  • 7
  • 17
  • NaN usually indicates a computation failure, looking here: http://stackoverflow.com/questions/28159572/localization-of-nan-in-dede-returns-nan-rather-than-n-def-in-windows-8-ser and http://stackoverflow.com/questions/34261938/what-is-the-difference-between-nan-nan-and-nan-nan – Petro Mar 17 '16 at 23:03
  • 1
    Please show us the whole code necessary to produce this log. – Bergi Mar 17 '16 at 23:04
  • @Bergi This is all relevant code. The rest has nothing to do with this issue. All this is specific to a certain chat site I use, which exposes a public API to read incoming messages and edit them as necessary. – Spans Mar 17 '16 at 23:13
  • What does console.log(conversions) immediately *before* the forEach() show? – nnnnnn Mar 17 '16 at 23:21
  • @Spans: So you say the same code does not show this behaviour on other sites? – Bergi Mar 17 '16 at 23:24
  • @nnnnnn It shows 11 objects as expected, except with two extra "epäluku undefined" strings at the end. The array itself doesn't contain these, I don't know why they are there. – Spans Mar 17 '16 at 23:25
  • @Bergi I cannot confirm that since this exact code cannot run on any other site. – Spans Mar 17 '16 at 23:26
  • Then please show us all the code that is necessary to run it so that it produces the problem. Which might include code from that site you're using your userscript on. – Bergi Mar 17 '16 at 23:31

1 Answers1

1

Turns out the issue was my code all along. In an unrelated function I was adding elements to the conversions array rather than the correct array within that function.

Spans
  • 364
  • 7
  • 17