考虑以下输入:

{
    "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()(但它已经是一个字符串!),在一个单独的环境中调用该函数,重写整个过程。

我不确定我在做什么错。

我将不胜感激任何帮助。作为一名初级开发人员,这可能是由于我尚不知道的数据类型古怪。

在此先感谢所有。

[编辑]要在评论中回答一些问题:

输入的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


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

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.


在调用graph_data.push()之后立即进行console.log(graph_data):

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()字符串作为值。我可以插入任何字符串作为键,但是它不会使用字符串作为值。

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

最佳答案

您的代码有点复杂,首先,您可以使用以下代码代替吗:



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);
    });
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





我相信这足以回答您的问题并解决这个奥秘:)

祝好运!

07-24 09:50
查看更多