【问题标题】:how to check the value of an NaN error如何检查 NaN 错误的值
【发布时间】:2016-03-27 17:25:17
【问题描述】:

嗨,所以我的 logs.date 打印出日志的实际日期,但是当我推送它时 进入一个名为日期的新数组,它在该数组的每个索引处都给了我 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;
                }
            });

【问题讨论】:

  • 您能告诉我们您是如何实例化dates 变量的吗?
  • chris walsh 我就像他的 var dates = [];
  • amanuel,日志只是我的一些陈述。它们包含一些内容,包括日期。
  • var dates = []; 看起来不错,但将其包含在代码示例中以及可能影响代码的任何其他内容(例如您的日志对象)中会有所帮助
  • 你是对的。我不确定我可以改变它,但知道。但是你知道发生了什么吗?

标签: javascript foreach indexof


【解决方案1】:

如果logs.dateundefined,那么递增它会给你一个NaN 值。

> undefined++
< NaN

【讨论】:

    猜你喜欢
    • 2010-10-30
    • 2012-02-05
    • 2018-09-28
    • 2015-03-26
    相关资源
    最近更新 更多