I have a large array, named data
, of string values from an external source, where each is supposed to represent an integer.
When I leave it to Javascript's automatic type conversion, there were no Javascript errors when I ran some arithmetic calculations on the whole array.
However, an operation like if (data[i] > data[i-1]) count++;
gives the wrong result for some of the array entries. I don't know which.
When I added the following manual conversion step, then computation on the array of numbers gave the expected result:
data.forEach((n, i) => {
data[i] = Number(n);
});
In other words, the same computation on data
gives different outcomes when the above step is included or not.
As the array is large, it is not practical for me to comb through the data to pick out the offending ones. I want to find out where the problem is. I modified the forEach
loop to:
data.forEach((n, i) => {
if (data[i] != Number(n)) console.log("!!!", i, n);
data[i] = Number(n);
});
but nothing was printed out.
What is a method to find out which values of the array are problematic?
data
is like: [ "123", "234", "456", "789", .... ]
.
If you want to know how I knew there was a problem, a colleague used Python on the same set of data and produced a different result. That led to an investigation.