Get data from an array of objects

Normally I'd reply with a bit of Jsonata that results in the same output... but lately I've been converting my code to a more "functional" style, to minimize looping and global function variables...

So if you will allow me try my hand at some javascript using array mappings instead:

// Extract rain data from API
let rain = msg.payload.map( data => {
    return {
        x: data.observation_time.value,
        y: data.precipitation.value.toFixed(1)
    };
});

// Extract temperature data from API
let temp = msg.payload.map( data => {
    return {
        x: data.observation_time.value,
        y: data.temp.value.toFixed(1)
    };
});
    
//Build chart Structure
msg.payload = [
    {
        series: [ "Rain", "Temp" ],
        data: [ rain, temp ]
    }
];
     
return msg;
3 Likes

The rain arrived 4 minutes early!!
Still not bad as Climacell predicted this on the chart at lunchtime today.

rain

2 Likes

Add an offset? :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.