# Find and count number of equal in array

**URL:** <https://discourse.nodered.org/t/find-and-count-number-of-equal-in-array/49460>\
**Category:** General\
**Created:** [7 August 2021 20:00 UTC](https://discourse.nodered.org/t/find-and-count-number-of-equal-in-array/49460 "2021-08-07T20:00:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [7 August 2021 20:00 UTC](https://discourse.nodered.org/t/find-and-count-number-of-equal-in-array/49460/1 "2021-08-07T20:00:36Z")

</div>

Hi

I need to count the numbers of Offline state in an array

I did find a way to use the find command, but it only takes the first one and then stops, how do I find all of them.

Next question:  
how to I insert the number of offline in to a value, so in this example a value of 2

This is my output from my SQL database

 ![Udklip](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/71f52ced6f12c17a0261ef786c79831f5309d2f3.jpeg)

```auto
[{"id":"881f149cc637aa08","type":"tab","label":"Export from MySQL","disabled":false,"info":"Export from MySQL"},{"id":"70cd0c5b25c1ba07","type":"inject","z":"881f149cc637aa08","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":260,"wires":[["e9561081af7aa794"]]},{"id":"e9561081af7aa794","type":"function","z":"881f149cc637aa08","name":"Hent Status på Ballast 10 (9)","func":"area = \"15\" ;\n\nmsg.topic = \"SELECT a.state, a.ballast, a.area, a.created_at FROM DC46BN59 a inner join (select ballast, max(created_at) as ts from DC46BN59 group by ballast) as b on a.ballast = b.ballast and a.created_at = ts and a.area = \"+area+\"\";\nreturn msg;\n\n\n\n//[{\"state\":\"Offline\",\"ballast\":\"14\",\"area\":3,\"created_at\":\"2021-08-07T17:04:36.000Z\"},{\"state\":\"Offline\",\"ballast\":\"7\",\"area\":3,\"created_at\":\"2021-08-07T17:06:43.000Z\"},{\"state\":\"Offline\",\"ballast\":\"10\",\"area\":3,\"created_at\":\"2021-08-07T19:00:22.000Z\"}]\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":260,"wires":[["91c645589080e501"]]},{"id":"91c645589080e501","type":"mysql","z":"881f149cc637aa08","mydb":"8e1cfeb8.d5134","name":"MySQL","x":740,"y":260,"wires":[["86744ad0c14e29cc","ef1b6ea9fd8854d2"]]},{"id":"f2b623058d5b8085","type":"debug","z":"881f149cc637aa08","name":"output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1110,"y":260,"wires":[]},{"id":"86744ad0c14e29cc","type":"function","z":"881f149cc637aa08","name":"","func":"\n\nstate = \"Offline\";\n\nmsg.output = msg.payload.find((el => el.state === state))\ncount = msg.output.state; //Numbers of offline devices\nmsg.payload = count;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":260,"wires":[["f2b623058d5b8085"]]},{"id":"ef1b6ea9fd8854d2","type":"debug","z":"881f149cc637aa08","name":"Raw output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":180,"wires":[]},{"id":"8e1cfeb8.d5134","type":"MySQLdatabase","name":"","host":"127.0.0.1","port":"3306","db":"nodered","tz":"","charset":"UTF8"}]

```

 ![Udklip2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/999976be68b33adcbf73d324054a3600a179563e.jpeg)

This is my output so fare  
 ![Udklip3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/41e1bf4b6912c42119ee7241d05f6f426512dfc6.jpeg)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [7 August 2021 20:22 UTC](https://discourse.nodered.org/t/find-and-count-number-of-equal-in-array/49460/2 "2021-08-07T20:22:02Z")

</div>

> [@zamzon](#):
>
> did find a way to use the find command, but it only takes the first one and then stops,

yes ,, `find` finds the first match and stops .. Use `filter` command instead

> **[Array.prototype.filter() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)**
>
> The filter() method of Array instances creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

Regarding the question of counting you could use `forEach` instead to loop through all the elements of your array and for each match add 1

```auto
let count = 0;

msg.payload.forEach(el => {
    if (el.state === "Offline") count += 1; //Numbers of offline devices
})

msg.payload = count;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 August 2021 20:37 UTC](https://discourse.nodered.org/t/find-and-count-number-of-equal-in-array/49460/3 "2021-08-07T20:37:28Z")

</div>

Have a look at Array reduce(). That will allow you to count matching entries. [JavaScript Array reduce() Method](https://www.w3schools.com/jsref/jsref_reduce.asp)

As for the second question, it is best not to ask unrelated questions in one post. It just gets confusing.

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [7 August 2021 20:44 UTC](https://discourse.nodered.org/t/find-and-count-number-of-equal-in-array/49460/4 "2021-08-07T20:44:33Z")

</div>

You are the man, Thanks  
It works perfect 🙂

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 August 2021 08:56 UTC](https://discourse.nodered.org/t/find-and-count-number-of-equal-in-array/49460/5 "2021-08-08T08:56:53Z")

</div>

Or using `reduce()`

```auto
msg.payload = msg.payload.reduce(accumulate, 0)
return msg;

// adds 1 if offline, 0 if online
function accumulate(total, thisOne) {
    return total + (thisOne.state == "Offline" ? 1 : 0)
}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 August 2021 15:16 UTC](https://discourse.nodered.org/t/find-and-count-number-of-equal-in-array/49460/6 "2021-08-08T15:16:53Z")

</div>

Or more concisely but more complex syntax

```auto
msg.payload = msg.payload.reduce((accumulator, currentValue) => {
        return accumulator + (currentValue.state == "Offline" ? 1 : 0)
    }, 0)
return msg;

```

---

<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:** [22 August 2021 15:17 UTC](https://discourse.nodered.org/t/find-and-count-number-of-equal-in-array/49460/7 "2021-08-22T15:17:39Z")

</div>

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