增量运算符返回 NaN

发布于 2024-12-18 15:12:31 字数 408 浏览 3 评论 0原文

我尝试使用 ++ 运算符递增变量,但结果总是得到 NaN,我不确定为什么。这是我的代码:

var wordCounts = { };
var x = 0
var compare = "groove is in the heart";
        var words = compare.split(/\b/);
        for(var i = 1; i < words.length; i++){
            if(words[i].length > 2){
                wordCounts["_" + words[i]]++;
            }
        }


alert(wordCounts.toSource());

I am trying to increment a variable using the ++ operator but I keep getting NaN as a result and I'm not sure why. Here is my code:

var wordCounts = { };
var x = 0
var compare = "groove is in the heart";
        var words = compare.split(/\b/);
        for(var i = 1; i < words.length; i++){
            if(words[i].length > 2){
                wordCounts["_" + words[i]]++;
            }
        }


alert(wordCounts.toSource());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妞丶爷亲个 2024-12-25 15:12:32

您基本上所做的是

undefined++

这将导致...

NaN

尝试...

wordCounts["_" + Words[i]] = ( wordCounts["_" + Words[i]]++ || 1);

由于 NaN 是一个“falsey”值,因此 ||将回落至1。

What you're basically doing is

undefined++

Which will result in...

NaN

Try...

wordCounts["_" + words[i]] = (wordCounts["_" + words[i]]++ || 1);

Since NaN is a "falsey" value the || will fall back to 1.

飘逸的'云 2024-12-25 15:12:32

为了能够使用 ++ 运算符(它接受一个数字并将其加一),目标首先需要有一个数字。

尝试检查对象是否已定义,如果未定义,则通过将其值设置为 1 来初始化它。

if ('undefined' === typeof wordCounts["_" + words[i]]) {
            wordCounts["_" + words[i]] = 0;
}

类似于:

var wordCounts = {};
var x = 0
var compare = "groove is in the heart";
var words = compare.split(/\b/);
for (var i = 1; i < words.length; i++) {
    if ('undefined' === typeof wordCounts["_" + words[i]]) {
        wordCounts["_" + words[i]] = 0;
    }
    if (words[i].length > 2) {
        wordCounts["_" + words[i]]++;
    }
}
alert( JSON.stringify( wordCounts ) );

To be able to use the ++ operator (which takes a number and increments it by one) the target needs to have a number first.

Attempt a check to see if the object is defined, and if not initialize it by setting it's value to 1.

if ('undefined' === typeof wordCounts["_" + words[i]]) {
            wordCounts["_" + words[i]] = 0;
}

Something like:

var wordCounts = {};
var x = 0
var compare = "groove is in the heart";
var words = compare.split(/\b/);
for (var i = 1; i < words.length; i++) {
    if ('undefined' === typeof wordCounts["_" + words[i]]) {
        wordCounts["_" + words[i]] = 0;
    }
    if (words[i].length > 2) {
        wordCounts["_" + words[i]]++;
    }
}
alert( JSON.stringify( wordCounts ) );
世俗缘 2024-12-25 15:12:32

您试图增加一个对象(wordCounts[]++),它不是一个数字,因此它不能增加,这就是您收到该错误的原因。你实际上想做什么(用简单的英语)?

You're trying to increment an object (wordCounts[]++) it's not a number so it can't be incremented, which is why you're getting that error. What are you actually trying to do (in plain English)?

冷︶言冷语的世界 2024-12-25 15:12:31

wordCounts["_" + Words[i]] 的值最初是 undefined,因此当您对它进行 ++ 操作时,它会给出 NaN。只需将您的代码更改为:

if (wordCounts["_" + words[i]]) {
    wordCounts["_" + words[i]]++;
} else {
    wordCounts["_" + words[i]] = 1;
}

The value of wordCounts["_" + words[i]] is initially undefined so when you ++ it, it gives you NaN. Just change your code to:

if (wordCounts["_" + words[i]]) {
    wordCounts["_" + words[i]]++;
} else {
    wordCounts["_" + words[i]] = 1;
}
月光色 2024-12-25 15:12:31

尝试类似...

var key = "_" + words[i];

if (wordCounts[key]) {
    wordCounts[key]++
} else {
    wordCounts[key] = 1;
}

您正在尝试增加 undefined ,这会给您 NaN

Try something like...

var key = "_" + words[i];

if (wordCounts[key]) {
    wordCounts[key]++
} else {
    wordCounts[key] = 1;
}

You are trying to increment undefined which is giving you NaN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文