如何检查NaN错误的值

时间:2016-03-27 17:25:17

标签: javascript foreach indexof

嗨所以我的logs.date打印出日志的实际日期,但是当我按下它时  进入一个名为dates的新数组,它在该数组的每个索引处给出了Nan。这是我第一次使用forEach循环和indexOf函数。你们中的任何一个人都看错了吗?

这是我的代码:

theLogs.forEach(function(logs){
                //if this line gets you 0 or higher it found it at that index, if you get -1 it was not found.
                var index = dates.indexOf (logs.date);
                if (index > -1){
                    //it was found so increment
                    dates[index]++;
                }
                else {
                    //it was not found so add a new entry in dates
                    dates.push(logs.date);

                    counters[counter] = 1;
                }
            });

1 个答案:

答案 0 :(得分:0)

如果logs.dateundefined,那么递增它将为您提供NaN值。

> undefined++
< NaN