IR command creation

I have the following function node that takes source11,a global variable, splits the numbers out, matches them to their respective IR commands for each number and then sends that on. I create a global variable called matchedCommands11. Right now my source11 global variable is set to 111; however, my matchedCommands11 global variable is set to the IR commands for 22. My function node code below is not updating the matchedCommands11 variable with the correct value in this case it should be array(0:)1 array(1:) 1 and array(2:) 1. Can anyone tell me what I'm doing wrong?

matchedCommands11 array[2]
0: "1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351"

1: "1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351"

[{"id":"8e9589381134810a","type":"function","z":"bfa8244b190d540b","name":"Split digits/Assign IR","func":"// Define IR commands array\nvar irCommands = [\n    // ir command 0\n    \"1,58000,1,37,23,351,23,164,23,94,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,94,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n    // ir command 1\n    \"1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n    // ir command 2\n    \"1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n    // ir command 3\n    \"1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n    // ir command 4\n    \"1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n    // ir command 5\n    \"1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n    // ir command 6\n    \"1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n    // ir command 7\n    \"1,58000,1,37,23,351,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n    // ir command 8\n    \"1,58000,1,37,23,351,23,164,23,164,23,94,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\",\n    // ir command 9\n    \"1,58000,1,37,23,351,23,164,23,164,23,94,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351\"\n    // Add more commands as needed\n];\n\n// Get the value of source11 from the message payload\nvar source11 = msg.payload;\n\nnode.warn('Source11 value: ' + source11);\n\n// Check if source11 is a number\nif (typeof source11 === 'number') {\n    // Convert source11 to a string to handle multi-digit numbers\n    var source11String = source11.toString();\n\n    node.warn('Source11 String: ' + source11String);\n\n    // Convert source11String to an array of digits\n    var numberArray = source11String.split('').map(Number);\n\n    node.warn('Number Array: ' + numberArray);\n\n    // Initialize an array to store matched IR commands\n    var matchedCommands11 = [];\n\n    // Iterate over each digit in the number array\n    numberArray.forEach(function(digit) {\n        // Ensure the digit is within the range 0-9\n        if (digit >= 0 && digit <= 9) {\n            // Get the corresponding IR command for the digit\n            var command = irCommands[digit];\n            // Push the command to the array of matched commands\n            matchedCommands11.push(command);\n        } else {\n            node.warn(\"Invalid digit: \" + digit);\n        }\n    });\n\n    node.warn('Matched Commands: ' + JSON.stringify(matchedCommands11));\n\n    // Store the array of matched commands in the global context\n    global.set(\"matchedCommands11\", matchedCommands11);\n} else {\n    // Handle the case when source11 is not a number\n    node.error(\"Default channel must be a number.\");\n}\n\n// Return the message\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2200,"y":880,"wires":[["5dde29537c64f323"]]}]

I suspect you are missing the fact arrays are zero based.

e.g, your array is as below (modified for readability)

var irCommands = [
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J"
    // Add more commands as needed
];

So when you request elements 1, 1 ,1 you get ['B', 'B', 'B']

If you were expecting ['A', 'A', 'A'] then you need to deduct 1 from the input value for a zero based index offset.

A better solution would be to use a look up object instead of an array.
And since this is node-red, a low/no-code solution might be better still

Example:

[{"id":"bff5a75ef0c73b9c","type":"inject","z":"339ffcc9e7def6f1","name":"setup IR commands","props":[{"p":"payload"},{"p":"payload.0","v":"data,for,IR0","vt":"str"},{"p":"payload.1","v":"data,for,IR1","vt":"str"},{"p":"payload.2","v":"data,for,IR2","vt":"str"},{"p":"payload.3","v":"data,for,IR3","vt":"str"},{"p":"payload.4","v":"data,for,IR4","vt":"str"},{"p":"payload.5","v":"data,for,IR5","vt":"str"},{"p":"payload.6","v":"data,for,IR6","vt":"str"},{"p":"payload.7","v":"data,for,IR7","vt":"str"},{"p":"payload.8","v":"data,for,IR8","vt":"str"},{"p":"payload.9","v":"data,for,IR9","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":680,"y":560,"wires":[["352b98a5674c30c1"]]},{"id":"352b98a5674c30c1","type":"change","z":"339ffcc9e7def6f1","name":"","rules":[{"t":"set","p":"IR_COMMANDS_V1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":560,"wires":[[]]},{"id":"c93940175dfb0d6b","type":"inject","z":"339ffcc9e7def6f1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"012","payloadType":"str","x":630,"y":620,"wires":[["daa7aaf64069a806"]]},{"id":"daa7aaf64069a806","type":"split","z":"339ffcc9e7def6f1","name":"","splt":"1","spltType":"len","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":760,"y":620,"wires":[["409aaa0c260156c2"]]},{"id":"409aaa0c260156c2","type":"change","z":"339ffcc9e7def6f1","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"IR_COMMANDS_V1[msg.payload]","tot":"flow"},{"t":"set","p":"complete","pt":"msg","to":"msg.parts.count = (msg.parts.index + 1) ? true : undefined","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":620,"wires":[["99ae9d07387c016e","44a1e9a458a75d7c"]]},{"id":"99ae9d07387c016e","type":"join","z":"339ffcc9e7def6f1","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1090,"y":620,"wires":[["08340ddd25080ea3"]]},{"id":"08340ddd25080ea3","type":"debug","z":"339ffcc9e7def6f1","name":"debug 430","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1250,"y":620,"wires":[]},{"id":"3d57a7ab93a1e6d7","type":"inject","z":"339ffcc9e7def6f1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"111","payloadType":"str","x":630,"y":660,"wires":[["daa7aaf64069a806"]]},{"id":"656f6dac655f4972","type":"inject","z":"339ffcc9e7def6f1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"987","payloadType":"str","x":630,"y":700,"wires":[["daa7aaf64069a806"]]},{"id":"44a1e9a458a75d7c","type":"debug","z":"339ffcc9e7def6f1","name":"debug 431","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1040,"y":720,"wires":[]}]