javascriptarraysfunctionreturnuint8array

Return Uint8Array from function


I'm trying to combine two Uint8Array and return the result in this function

commandframe = new Uint8Array([0x01,0x00]); GetCRC16Full(commandframe, true);

function GetCRC16Full(cmd, IsHighBefore) {
    let check= [0xff];

    var mergedArray = new Uint8Array(cmd.length + check.length);
    mergedArray.set(cmd);
    mergedArray.set(check, cmd.length);
    
    return mergedArray;

}

in Vscode debug mode I can see in function mergedArray is [0x01,0x00,0xff], but after return commandframe doesn't change, why is that?


Solution

  • You are never modifying commandFrame. The mergedArray is returned from the function but is not assigned to commandFrame

    you can try this:

    commandframe = new Uint8Array([0x01,0x00]); 
    commandframe = GetCRC16Full(commandframe, true);