How to create an array from mqtt input

I have a button (on/off) - connected to another node that only shows msg when it is turned on.

I have another mqtt in, that is connected along with the first node to a function node.

What I would like to achieve in node-red is to get the max of all the input msg from mqtt in when the button is on.

I have tried to use the code below in a function but it seems doesn't show the output as array when I check the debug output:

var newmsg = {lit:msg.payload};
if( context.global.stat =="on")
{
    var litarray = []; //define empty array to get max later
    while(context.global.stat =="on")
    {
    litarray.push(newmsg);
    return[litarray,null];
    }
        
}

if (context.global.stat == "off")
{
    return[null, null]; 
}

Look at the docs https://nodered.org/docs/writing-functions#storing-data to see the standard way of getting and setting a global variable within a function node

Thank you @ukmoose my question is regarding the array specifically, but this gave a good idea that I can implement by continuously comparing the last and the current values and get the max from them! thank you.

Also will be viewing the context part in case I am missing something and will get back to you in case I am stuck again :slight_smile: