# Number of times a value appears in a object

**URL:** <https://discourse.nodered.org/t/number-of-times-a-value-appears-in-a-object/54833>\
**Category:** General\
**Tags:** http-request, database, function-node\
**Created:** [7 December 2021 17:27 UTC](https://discourse.nodered.org/t/number-of-times-a-value-appears-in-a-object/54833 "2021-12-07T17:27:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DVSL](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@DVSL](https://discourse.nodered.org/u/DVSL)\
**Post date:** [7 December 2021 17:27 UTC](https://discourse.nodered.org/t/number-of-times-a-value-appears-in-a-object/54833/1 "2021-12-07T17:27:31Z")

</div>

Hi, hope this find you well. I have an object like the one in the image bellow, and I would like to know the number of times a value appear in a certain key. So, In the key "Type" I would like to have an output like "Lemon : 4 times" or if it's with the key "Price" it should be like "12: 3 times, 15:1 time".

Any help will be appreciate it. Thank you.

 ![Screenshot 2021-12-07 132604](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/03a00bd453f03f30f08824abaa70e45a6a2ebd40.png)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [7 December 2021 20:57 UTC](https://discourse.nodered.org/t/number-of-times-a-value-appears-in-a-object/54833/2 "2021-12-07T20:57:08Z")

</div>

Welcome to the forum!

I was waiting for someone to provide a Javascript `function` node example, that can iterate over all of the fields and summarize by value... however, since that has not happened, here is the way I would do it using a `change` node with a JSONata expression (which is a rather advanced lambda syntax, but ideal for just this situation).

If you know which fields you want to aggregate, it can be as simple-looking as this:

```auto
payload.* {
    "FARMER": { FARMER: $count(FARMER) },
    "TYPE": { TYPE: $count(TYPE) },
    "PRICE": { $string(PRICE): $count(PRICE) }
}

```

which is human-readable terms means: for each object in the payload fields, count each field by value"

Since you did not include any actual data in your post, I did not spend the time to type it in so I could actually test it out -- but that should be close to correct. Just make sure to choose the `J:` expression option in the pulldown for the `change` node.

If you get stuck, post some sample data so we can try to help you out some more...

---

<div class="post-metadata">

**Author:** ![DVSL](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@DVSL](https://discourse.nodered.org/u/DVSL)\
**Post date:** [7 December 2021 22:46 UTC](https://discourse.nodered.org/t/number-of-times-a-value-appears-in-a-object/54833/4 "2021-12-07T22:46:07Z")

</div>

Thanks for the explanation, I really appreciate your reply.

I'm taking this data from Firebase using a http request node. I read the data in an object format but also I can convert it to json using the json node. My data looks like that:

```auto
{"-MqIOHLFfa8h4qrt4olG":{"FARMER":"EMILIO FELIZ","PRICE":12,"TYPE":"LEMON"},"-MqKOD_jSNKhsxgeWswx":{"FARMER":"EMILIO FELIZ","PRICE":12,"TYPE":"LEMON"},"-MqLVRi5ppVR0bAbg05W":{"FARMER":"MISAEL DE LOS SANTOS","PRICE":13,"TYPE":"LEMON"},"-MqLVYxo2tAKeBNlchbN":{"FARMER":"MISAEL DE LOS SANTOS","PRICE":13,"TYPE":"APPLE"}}

```

Again, thanks for the reply.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [7 December 2021 23:59 UTC](https://discourse.nodered.org/t/number-of-times-a-value-appears-in-a-object/54833/5 "2021-12-07T23:59:13Z")

</div>

So did you try using that expression inside a `change` node?

Here's what it looks like in my editor:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9f7a41a2dc59c7a8996a0ba2a450f677a958579b.png)

---

<div class="post-metadata">

**Author:** ![DVSL](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@DVSL](https://discourse.nodered.org/u/DVSL)\
**Post date:** [8 December 2021 13:40 UTC](https://discourse.nodered.org/t/number-of-times-a-value-appears-in-a-object/54833/6 "2021-12-08T13:40:54Z")

</div>

It works perfectly. Thank you so much.

---

<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:** [6 February 2022 13:41 UTC](https://discourse.nodered.org/t/number-of-times-a-value-appears-in-a-object/54833/7 "2022-02-06T13:41:35Z")

</div>

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