Javascript parseFloat 和 nulls

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15478609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:55:24  来源:igfitidea点击:

Javascript parseFloat and nulls

javascriptparsingnullhighchartsparsefloat

提问by Palendrone

I am very new to javascript as I am currently making a cross platform web app in jQuery Mobile, I have used the example of XML Parsing to a HighCharts graph yet when I encounter a null in my series data it fails to draw any of the line and makes it into a scatter plot almost.

我对 javascript 非常陌生,因为我目前正在 jQuery Mobile 中制作一个跨平台的 Web 应用程序,我已经使用 XML 解析到 HighCharts 图表的示例但是当我在我的系列数据中遇到空值时,它无法绘制任何一条线并使其几乎成为散点图。

// push data points
$(series).find('data point').each(function(i, point) {
    seriesOptions.data.push( 
        parseFloat($(point).text())
    );
});

I have no idea how to write a if statement that checks to see if it found a null and if so how to tell it to use it... Can anyone please help or point me in the right direction as I would love my charts to be correct rather than placing a zero value where I have a null.

我不知道如何编写一个 if 语句来检查它是否找到了空值,如果是,如何告诉它使用它......任何人都可以帮助我或指出正确的方向,因为我希望我的图表能够是正确的,而不是在我有一个空值的地方放置一个零值。

采纳答案by Palendrone

With a quick google on Javascript If statments I beleive I have got there - thanks Bjorn :0) Your answer led me to get there !!!

在 Javascript 上快速谷歌一下如果我相信我已经到了那里 - 谢谢 Bjorn :0) 你的回答让我到了那里!

// push data points
$(series).find('data point').each(function(i, point) {
    var floatVal = parseFloat($(point).text());
            if (!isNaN(floatVal)) {
                seriesOptions.data.push(floatVal);
        }
        else {
        seriesOptions.data.push(null);
        }
        console.log(floatVal)
    });

回答by Bj?rn Roberg

Well, parseFloat will return 'NaN' if it's not a number (null and undefined are NaNs) so you could try doing like this:

好吧,如果 parseFloat 不是数字(null 和 undefined 是 NaN),则 parseFloat 将返回“NaN”,因此您可以尝试这样做:

// push data points
$(series).find('data point').each(function(i, point) {
    var floatVal = parseFloat($(point).text());
    if (!isNaN(floatVal)) {
        seriesOptions.data.push(floatVal);
    }
});

回答by Grant Thomas

A nullcheck in JavaScript if just like any other C-style language:

一个null在JavaScript中,如果就像任何其他C风格的语言检查:

 if (thing == null) 

Or

或者

 if (thing != null)

I find this works well in most cases against my own programming where I'm writing as I would in, say, C#; however I find other peoples code relies on things never having been declared or set and such and so, and, all in all, it boils down to a spaghetti of checking for nulland "undefined"- yes, the literal string, really - and whatever else.

我发现这在大多数情况下都适用于我自己的编程,就像我在 C# 中编写的那样;但是我发现其他人的代码依赖于从未被声明或设置过的东西等等,总而言之,它归结为检查null和的意大利面条"undefined"- 是的,文字字符串,真的 - 以及其他任何东西。