0

I would like to know the length of an array read inside a function in NodeRed using JavaScript but it is not displaying/returning any value. Can someone help me here?

enter image description here

Here is the code inside the function block in Node-Red

let j = 0;
let array1 = { payload: msg.payload };

j = array1.length;

return j;

I do not see any return value for j. any help?

I was expecting a value of 50 for j to be displayed on NodeRed debug console.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    You are not showing enough of your code for us to be able to help. Please see https://stackoverflow.com/help/how-to-ask – Ruan Mendes Apr 13 '23 at 17:04
  • 1
    Array1 isn’t an array – James Apr 13 '23 at 17:26
  • 1
    `array1` is an [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object), not an [array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). It may be wise to peruse some documentation on the basics. – Heretic Monkey Apr 13 '23 at 18:29
  • Sorry, i did not know that array1 is an object. I am a beginner. Can some one help me how to get length or how many items are inside an object ? – Hazarath Voleti Apr 13 '23 at 18:47
  • We have many questions about that: [How to efficiently count the number of keys/properties of an object in JavaScript](https://stackoverflow.com/q/126100/215552) – Heretic Monkey Apr 13 '23 at 18:59
  • Also Node-RED Function nodes **MUST** return an `msg` object, not a bare value. If `j` was 50 this would be a raw number, the correct return is to set `msg.payload = j` then `return j` – hardillb Apr 13 '23 at 20:41

1 Answers1

-1

Here is the actual answer to your question. Note these things given below;

  1. let array1 = { payload: msg.payload } is not an array. It is an object. You cannot find the length of the object by obj.length; rather use Object.keys(array1).length

If you want to find the length (number of properties) of the object then use the following code snippet.

let array1 = { payload: msg.payload };
let length = Object.keys(array1).length;
console.log(length);


// Example
let person = {name: "Mehdi", city: "Jamshoro", country: "PK"}
let length = Object.keys(person).length;
console.log(length);

// Output: 3

The length of an array can be found in this way:

let arr = [1,2,3,4,5,6]
console.log(arr.length)

// Output: 6

It seems like you are using a return statement outside the function. return does not work outside the function. Use console.log() instead.

  • 1
    Objects do not have a length; that's why they don't have a `length` property. `Object.keys(obj).length` merely gives the number of properties the object itself defines (not including those defined by any objects further up the prototype chain). So I would say this answer is incorrect in its definition of an object's "length". – Heretic Monkey Apr 13 '23 at 18:57
  • Object length means, the number of properties the object has. `Object.keys(obj).length` return `3` if the object has `3` properties and so. – Saqlen Mehdi Apr 13 '23 at 19:12
  • No, it doesn't. Asking how long an object is is like asking how wide green is. – Heretic Monkey Apr 14 '23 at 14:09
  • Even after using the below code, I still see a value of 1 instead of 50. Any reason ? let length = Object.keys(array1).length; console.log(length); Here is what I see as an output of array1 : [enter image description here][1] [1]: https://i.stack.imgur.com/5AC7b.png – Hazarath Voleti May 02 '23 at 12:37
  • @Heretic Monkey , I think you are correct. I always get a value of 1 in the console. Do you know how to solve this ? I am expecting a value of 50 – Hazarath Voleti May 02 '23 at 12:38