【问题标题】:JavaScript: push()ing a string into array is returning **null**JavaScript:将字符串 push() 到数组中返回 **null**
【发布时间】:2018-07-17 22:23:45
【问题描述】:

考虑以下输入:

{
    "algorithm_run": "mean: 07/02/2018 02:38:09F1 (Mhz) mean",
    "id": 12,
    "parameter": "F1 (Mhz) mean",
    "runs": "2017-09-19 15:41:00(0:00:59.350000)",
    "start_time": "2017-09-19 15:41:00",
    "value": 13.5629839539749
}

在包含该 JSON 的 URL 上调用以下函数:

function getScatterPlotRunData(url_string) {
    $.getJSON(url_string, function(data) {
        var graph_data = [];
        for (i = 0; i < data.length; i++) {
            graph_data.push({
                "x" : i,
                "y" : data[i].value,
                "z" : data[i].start_time
            });
        };
        drawScatterPlot(graph_data, data[0].parameter);
    });
}

这个想法是建立一个数组:

{
    x: index of loop,
    y: value from data[i].value,
    z: time stamp string from data[i].start_time
}

我当前的输出将时间戳返回为 null

{
    x: 0
    y: 13.5629839539749
    z: null
}

我尝试了很多东西。转动 start_time.toString() (但它已经是一个字符串!),在单独的环境中调用函数,重写整个东西。

我完全不确定自己在做什么错。

我将非常感谢任何帮助。这可能是由于我作为初级开发人员还不知道的数据类型怪癖。

在此先感谢大家。

[编辑]回答cmets中的一些问题:

传入的 JSON 是有效的。实际上,帖子顶部的输入是从控制台复制的。我刚刚删除了网址。

在几个阶段登录到控制台会显示以下内容:

console.log(data) 在定义函数之后:

"$.getJSON(url_string, function(data) {
    console.log(data);
    ... // Rest of code
    // Reveals entire valid JSON, with all fields as they should be. EG: 
    >>> start_time": "2017-09-19 15:41:00" // not NULL

console.log(data) 在 for 循环之后和循环内部:

for (i = 0; i < data.length; i++) {
    console.log(data);
    ...
    // Reveals entire JSON, with all fields as they should be. EG: 
    >>> start_time": "2017-09-19 15:41:00" // not NULL

    console.log(typeof data);
    >>> object // Still valid JSON

    console.log(typeof graph_data);
    >>> object // Array is type object.

    console.log(graph_data);
    >>> [] // Array is empty.

console.log(graph_data) 在调用 graph_data.push() 之后:

graph_data.push({
    "x" : i,
    "y" : data[i].value,
    "z" : data[i].start_time
});
    console.log(graph_data);
    >>>0: {x: 0, y: 13.5629839539749, z: null}
    // All values for time stamp are now null.

经过刚才的进一步测试:

graph_data.push({
    "x" : i,
    "y" : data[i].value,
    "z" : i
});
    console.log(graph_data);
    >>>z: 1
    // I can insert as many Key : Value pairs as I want
    // As long as values are integers.

插入其他值:

graph_data.push({
    "x" : i,
    "y" : data[i].value,
    "z" : "Foo"
});
    console.log(graph_data);
    >>>z: null
    // Hard coding a "string" gives me a null value.

问题在于尝试将 .push() 字符串作为值。我可以插入任何字符串作为键,但它不接受字符串作为值。

关于为什么会这样的任何想法?

【问题讨论】:

  • 如果您在for 循环中添加console.log(data[i]),您会看到start_time 值吗?
  • 我猜你的输入没有像你期望的那样格式化。在添加到数组之前,您是否尝试过 console.log() 每个项目?它看起来像什么?
  • 我已经尝试过了,我现在将编辑问题。
  • 我无法重现此错误。请您仔细检查传入的 json 数据是否有效?
  • (1) 如果您的输入确实如您在问题开头所描述的那样,那么这永远不会起作用:它不是一个数组,但您将它视为一个数组。 (2) 你能用硬编码的 JSON 重现问题吗(而不是通过$.getJSON 回调)?如果是这样,你能创建一个说明它的小提琴吗?

标签: javascript jquery arrays json for-loop


【解决方案1】:

你的代码有点复杂,首先,你能不能改用下面的代码:

function convertCollectionElement(input, index){
    return {
      "x": index,
      "y": input.value,
      "z": input.start_time
    };
}

function convertCollection(inputs){
    return inputs.map(convertCollectionElement);
}

function checkForNullity(input, index){
    if(input.z === null){
      throw new Error("z was null on : "+JSON.stringify(input));
    }
}

function getScatterPlotRunData(url_string) {
    $.getJSON(url_string, function(data) {
        var graph_data = convertCollection(data);
        
        //result of conversion
        graph_data.forEach(checkForNullity);
        
        drawScatterPlot(graph_data, data[0].parameter);
        
        //verify that "drawScatterPlot" didn't mofidy our data
        graph_data.forEach(checkForNullity);
    });
}
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

我相信这足以回答您的问题并解开这个谜团:)

祝你好运!

【讨论】:

  • 嘿,所以,我刚回来试试这个。我完全按原样尝试了您的代码,但它仍然在我 100% 确定具有时间戳值的字段上返回 null。我自己检查了数据库。
  • 显然我的图表中的某些东西正在修改变量,但我不确定如何跟踪它。
  • 正如我所建议的 :) 很高兴我能提供帮助。跟踪可以通过 Object.defineProperty(el,"start_value",{ set: function(x){throw new Error();} }, get: function(){return val;} );见developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多