I want to know the length of an array read inside a function in NodeRed using JavaScript, but it doesn't display/return any value. Can anyone help me?
This is the code inside the function block in Node-Red
let j = 0; let array1 = { payload: msg.payload }; j = array1.length; return j;
I don't see any return value for j
. Any help?
I expected the value of j
to be displayed on the NodeRed debug console.
This is the actual answer to your question. Please pay attention to these matters given below;
let array1 = { payload: msg.payload }
is not an array. It is an object. The length of the object cannot be found viaobj.length
; instead useObject.keys(array1).length
If you want to find the length (number of properties) of an object, use the following code snippet.
The length of the array can be found by:
You appear to be using a
return
statement outside a function.return
Has no effect outside a function. Useconsole.log()
instead.