1

Considering the following input:

{
    "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
}

Calling the following function on the URL containing that JSON:

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

The idea is to build an array with:

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

My current output is returning the time stamp as null:

{
    x: 0
    y: 13.5629839539749
    z: null
}

I've tried many things. Turning start_time.toString() (but it's already a string!), calling the function on a separate environment, rewriting the whole thing.

I am not sure what I'm doing wrong here at all.

I would greatly appreciate any help. It's probably due to a data type quirkiness that I yet don't know, being a junior developer.

Thanks to all in advance.

[EDIT] To answer some questions in the comments:

The JSON conming in is valid. In fact, the input at the top of the post is copied from the console. I just deleted the URLs.

Logging to the console at several stages reveals the following:

console.log(data) right after defining the function:

"$.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) right after the for loop and inside the loop:

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) right after calling 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.

After further testing just now:

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.

Inserting other values:

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.

The problem lies into trying to .push() a string as the value. I can insert any string as the key, but it's not taking in strings for the value.

Any ideas as to why this is?

Mormoran
  • 751
  • 13
  • 34
  • If you add `console.log(data[i])` in the `for` loop you see the `start_time` value? – Peter Feb 07 '18 at 15:11
  • 1
    I guess your input is not formatted as you expect. Did you try to `console.log()` each item before adding to the array ? What does it look like ? – M'sieur Toph' Feb 07 '18 at 15:11
  • I've tried those, I'll edit the question now. – Mormoran Feb 07 '18 at 15:12
  • I think you're looping an object expecting it to behave as if it was an array. Please check [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – Álvaro González Feb 07 '18 at 15:15
  • `var graph_data = data.map(function(d, i) { return {x: i, y: d.value, z: d.start_time} });`` <-- what is the `graph_data ` value if you change the for `loop` to this? – Peter Feb 07 '18 at 15:31
  • 4
    I couldn't reproduce this error. Could you please double check if the incoming json data is valid? – Belian Feb 07 '18 at 15:31
  • 2
    (1) If your input is really as you describe at the start of your question, then this could never work: it is not an array, yet you treat it as one. (2) Can you reproduce the problem with a hard-coded JSON (instead of via a `$.getJSON` callback)? If so, can you create a fiddle that illustrates it? – trincot Feb 07 '18 at 16:12
  • @Mormoran please accept my answer if it has answered your stated question. If you're having trouble figuring out why graph library you're using is modifying your data - i suggest starting a new question and providing details about the graph library. Have a great day! – travnik Feb 19 '18 at 11:24

1 Answers1

1

Your code is a bit complex, first, could you use the following instead:

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>

I believe this will be sufficient to answer your question and resolve this mystery :)

good luck!

travnik
  • 690
  • 7
  • 18
  • Hey, so , I just came back to try this. I tried your code exactly as it was, and it's still returning null on fields that I am 100% sure have a time stamp value on them. I checked the DB myself. – Mormoran Feb 08 '18 at 15:48
  • Apparently something in my graph is modifying the variable, though I'm not sure how to track this. – Mormoran Feb 08 '18 at 16:08
  • As i suggested :) Glad I could help. Tracking might be done via Object.defineProperty(el,"start_value",{ set: function(x){throw new Error();} }, get: function(){return val;} ); see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty – travnik Feb 09 '18 at 01:40