Count objects and write the result

Hello!

There is a way to count objects and write the result into an object.

Yes but the solution depends on what & where these objects are (in a 1 dimension array? A multidimensional array, an object, a sub property of an object), whether the objects to be counted are nested, whether there are other properties that need including or excluding from the final count etc etc etc etc.

Is this a node-red question?

Hello!

There are only objects and it was shown how many there are that I can filter them further.

There is 14 objects on that picture you have shared.
But that obviously not the answer you are looking for. Please take a time and read what Steve wrote and rephrase the question by adding enough content.

Hello!

I want to count all objects and then write the result into a new object.

I want to calculate images, I need the number of objects. As with the picture, the result should be 14.

I was able to count 14 objects on that image just because you have shared the image. That image contains those objects. That makes boundaries.
Nobody can't count any object if the thing is not given where to count. (except when counting all objects in universe)

Hi again. Your question is ambiguous for several reasons. Never mind, I will make a bunch of assumptions. E.g. the source data is in msg.payload, you actually want to count keys not objects etc...

To count the properties (which are strings not objects) then use Object.keys(msg.payload).length

To put it in that into an object, you use standard JavaScript

E.g...

var len = Object.keys(msg.payload).length;
msg.payload = {};
msg.payload.length = len;
return msg;

... Though I fail to understand why you would want to do that.

1 Like

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