I have 8 channels of data coming into a function node, which is stored in flow conext as "gaugeData". This data is stored individually into it's own channel in flow context, added to a new array called CH1, CH2, ...CH8 etc. This data is then sent to a chart node on dashboard, where all 8 channels are displayed on the same chart.
//initialise arrays ch1, ch2.... ch8 = [ ];
var ch1, ch2 ... ch8 = [];
//Push each channel to its' own array
for(var j in gaugeData[1]){
ch1.push([j, gaugeData[1][j]]);
flow.set("ch1", ch1)
}
for(var k in gaugeData[2]){
ch2.push([k, gaugeData[2][k]]);
flow.set("ch2", ch2)
}
for(var l in gaugeData[3]){
ch3.push([l, gaugeData[3][l]]);
flow.set("ch3", ch3)
}
for(var m in gaugeData[4]){
ch4.push([j, gaugeData[4][m]]);
flow.set("ch4", ch4)
}
for(var n in gaugeData[5]){
ch5.push([j, gaugeData[5][n]]);
flow.set("ch5", ch5)
}
for(var o in gaugeData[6]){
ch6.push([o, gaugeData[6][o]]);
flow.set("ch6", ch6)
}
for(var p in gaugeData[7]){
ch7.push([p, gaugeData[7][p]]);
flow.set("ch7", ch7)
}
for(var q in gaugeData[8]){
ch8.push([q, gaugeData[8][q]]);
flow.set("ch8", ch8)
}
// set up chart data ignore the series names for now
msg.payload =[{
"series": [ "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7", "CH8"],
"data": [
ch1.map((element, j) => ({ x: (j * 10) + 10, y: element[1] }) ),
ch2.map((element, k) => ({ x: (k * 10) + 10, y: element[1] }) ),
ch3.map((element, l) => ({ x: (l * 10) + 10, y: element[1] }) ),
ch4.map((element, m) => ({ x: (m * 10) + 10, y: element[1] }) ),
ch5.map((element, n) => ({ x: (n * 10) + 10, y: element[1] }) ),
ch6.map((element, o) => ({ x: (o * 10) + 10, y: element[1] }) ),
ch7.map((element, p) => ({ x: (p * 10) + 10, y: element[1] }) ),
ch8.map((element, q) => ({ x: (q * 10) + 10, y: element[1] }) )
],
"labels": [ "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7", "CH8"]
}];
Obviously this is a ton of redundant code, what i'm trying to do is optimise it somewhat into something like:
for(var j = 1; j <= 8; j++){
for(var k in gaugeData[j]){
ch`${j}`.push([k, gaugeData[j][k]]);
flow.set(`ch+${[j]}`, ch`${[j]}`)
}
}
whereby I have the variable "j" in the array name, pushing to ch1, ch2 ... ch8 etc as it loops thorugh. But I can't figure out the backtick notation and wether its' even applicable here? Any guidance on how to achieve this would be appreciated.