# Count objects and write the result

**URL:** <https://discourse.nodered.org/t/count-objects-and-write-the-result/45368>\
**Category:** General\
**Created:** [8 May 2021 18:00 UTC](https://discourse.nodered.org/t/count-objects-and-write-the-result/45368 "2021-05-08T18:00:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [8 May 2021 18:00 UTC](https://discourse.nodered.org/t/count-objects-and-write-the-result/45368/1 "2021-05-08T18:00:35Z")

</div>

Hello!

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 May 2021 18:07 UTC](https://discourse.nodered.org/t/count-objects-and-write-the-result/45368/2 "2021-05-08T18:07:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [8 May 2021 18:10 UTC](https://discourse.nodered.org/t/count-objects-and-write-the-result/45368/3 "2021-05-08T18:10:47Z")

</div>

Hello!

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

 ![Bildschirmfoto 2021-05-08 um 20.09.13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cff303bd0690d6e2b13dac1c45b58896bbeab7d9.png)

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [8 May 2021 18:43 UTC](https://discourse.nodered.org/t/count-objects-and-write-the-result/45368/4 "2021-05-08T18:43:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![madmax](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madmax](https://discourse.nodered.org/u/madmax)\
**Post date:** [8 May 2021 18:49 UTC](https://discourse.nodered.org/t/count-objects-and-write-the-result/45368/5 "2021-05-08T18:49:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [8 May 2021 18:56 UTC](https://discourse.nodered.org/t/count-objects-and-write-the-result/45368/6 "2021-05-08T18:56:33Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 May 2021 19:15 UTC](https://discourse.nodered.org/t/count-objects-and-write-the-result/45368/7 "2021-05-08T19:15:47Z")

</div>

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...

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 July 2021 19:15 UTC](https://discourse.nodered.org/t/count-objects-and-write-the-result/45368/8 "2021-07-07T19:15:48Z")

</div>

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