Send data[i].key to function, on return, update data[i].key with new data?

Send data[i].key to function, on return, update data[i].key with new data?
0

#1

Hi,
New here I may not get all the terminology correct.

I have 10 dates that are sent to a graph.
At the moment my dates data is in this format 2018-11-19T10:40:54+01:0000Z

I have made a Function to convert it to just date = 2018-11-19

function convertTimestamp (timestamp)
{

    returnedArray = [];                                                                     // clear array

    console.log("timestamp FROM Node-RED = " + timestamp);                                  // Timestamp from Node-RED

    var date = moment(timestamp);
    var dateComponent = date.utc().format('YYYY-MM-DD');
    var timeComponent = date.utc().format('HH:mm:ss');
    console.log("Time and Date converted .... " + timeComponent + "  " + dateComponent);

    var timestamp_Formatted = dateComponent;
    console.log("Date converted ............. " + timestamp_Formatted );

    returnedArray.push(dateComponent);
    return returnedArray;

}

.
.
.

On return, I now need to update this line in my main JavaScript Function

I tried …

data[i].key = returnedArray;                                            // Returned result
console.log("timestamp back in Function as array  ... " + data[i].key); // Shows correct date !

 processed_json.push([data[i].key, data[i].value]);     // Shows on graph as 0 1 2 3 4 5 6 7 8 9, not as new dates ?

.
.

What am I doing wrong ?

Thanks


#2

@questuk,

what is the format of the data returned?

I see that you are logging out the key, but using the value.

Try logging out data[i].value and see what you get.


#3

Hi Tomer,
The value is correct = 0.001

This may help here is the function that gets data from my JSON file and places this onto a graph

$(function ()
{
    var processed_json = new Array(); // if you pass a number to the constructor, you will get an array of that length: x = new Array(5); alert(x.length); // 5

    $.getJSON('code/Data/Electricity_Key_Value.json', function (data)                       // TODO At he moment I have to copy this to this folder, better solution ?
    {

        for (i = 0; i < data.length; i++)                                                   // Populate series
        {

            convertTimestamp(data[i].key);                                                  // Send timestamp to be converted
            data[i].key = returnedArray;                                                    // Returned result
            console.log("timestamp back in Function as array  ... " + data[i].key);
            console.log("timestamp back in Function as array  ... " + data[i].value);

            processed_json.push([data[i].key, data[i].value]);                              //  This is original part of function.

        }


#4

Hi,

I have now resolved my problem.

returnedArray.push(dateComponent);
    return returnedArray;

changed code to


function convertTimestamp (timestamp)
{

    //returnedArray = [];                                                                     // clear array

    console.log("timestamp FROM Node-RED = " + timestamp);                                  // Timestamp from Node-RED

    var date = moment(timestamp);

    var dateComponent = date.utc().format('YYYY-MM-DD');
    var timeComponent = date.utc().format('HH:mm:ss');
    console.log("Time and Date converted .... " + timeComponent + "  " + dateComponent);

    var timestamp_Formatted = timeComponent;
    console.log("Date converted ............. " + timestamp_Formatted );

    result = timestamp_Formatted;
    return result;
}

.

Also the problem with the graph was, I returned a date example … 06-12-18
returned dates were all the same date, so graph could not do a x axis correctly.
I changed to time, as none were the same and chart updated. still more work to do but getting there :grinning:

Thanks