Subtraction between first and last element of array

Hello!
Sorry to bother for newb thing but I am unable to solve.
I have return below in to function node to perform subtraction between first and last element of array. it returns length and whole array but there is something wrong with var one=Firstagg2.payload[0]; and var last= Firstagg2.payload[newMsg-1]; I have right to get first and last element. It doesn't return anyone of it and hence subtraction.

Please Help me with this!

var newMsg = { payload: msg.payload.length };
//return [newMsg, msg];

var Firstagg2 = { payload: msg.payload };
//return Firstagg2;

var one=Firstagg2.payload[0];
var last= Firstagg2.payload[newMsg-1];
msg = last-one;

return msg;

I am able to read var one but still not able to do for var last...

var newMsg = { payload: msg.payload.length };
//return [newMsg, msg];

var Firstagg2 = { payload: msg.payload };
//return Firstagg2;
var one =  { payload: msg.payload[0]};
//return one;
var last =  { payload: msg.payload[newMsg-1]};
return last;

//msg= last-one;
//return msg;

You are over complicating it and creating unneeded objects

let array_length = msg.payload.length;
let one = msg.payload[0];
let last= msg.payload[array_length - 1];
msg.payload = last - one;
return msg;

Thank you !

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