Push to an Array with a variable in its' name

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.

ch would be better as an array.
e.g.

[{"id":"1f1081c5.e00e96","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":660,"wires":[["1e167e8c.63da19"]]},{"id":"1e167e8c.63da19","type":"function","z":"bf9e1e33.030598","name":"","func":"let gaugeData = [{\"one\":1,\"two\":2},{\"one\": 3,\"two\": 4}];\nlet ch  = []\nfor(var j = 0; j < gaugeData.length; j++){\n    ch[j] = [];\n    for(var k in gaugeData[j]){\n        ch[j].push([k, gaugeData[j][k]]);\n        node.warn(ch[j]) // set flow context here\n    }\n}\n//node.warn(ch);\nlet data = ch.reduce((acc, arr, index) => {\n    let maparray = arr.map((element, j) => ({ x: (j * 10) + 10, y: element[1] }) )\n    acc[index] = maparray;\n    return acc\n},[])\nlet series = ch.map((arr, index) => `CH${index+1}`);\n msg.payload =[{\n        \"series\": series,\n        \"data\": data,\n            \n        \"labels\": series\n    }];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":660,"wires":[["5f04a27c.be58ec"]]},{"id":"5f04a27c.be58ec","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":660,"wires":[]}]

I had to guess as to the input data. This should be dynamic to the input payload

// input data
let gaugeData = [{"one":1,"two":2},{"one": 3,"two": 4}];
// let gaugeData = msg.payload;
//initialise ch as array
let ch  = []
//loop throuh data to create ch and set flow data
for(var j = 0; j < gaugeData.length; j++){
    ch[j] = []; //initialise ch[j] to array
    for(var k in gaugeData[j]){
        ch[j].push([k, gaugeData[j][k]]);
        node.warn(ch[j]) // possibly set flow context here
        // flow.set(`ch${j}`, ch[j]);
    }
// or maybe set flow data here.
}
//create data for chart
let data = ch.reduce((acc, arr, index) => {
    let maparray = arr.map((element, j) => ({ x: (j * 10) + 10, y: element[1] }) )
    acc[index] = maparray;
    return acc
},[])
//create series and labels
let series = ch.map((arr, index) => `CH${index+1}`);
// create chart payload
msg.payload =[{
        "series": series,
        "data": data,
        "labels": series
    }];
return msg;

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