Find and count number of equal in array

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

[{"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"}]

This is my output so fare
Udklip3

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

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

let count = 0;

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

msg.payload = count;
return msg;

Have a look at Array reduce(). That will allow you to count matching entries. JavaScript Array reduce() Method

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

You are the man, Thanks
It works perfect :slight_smile:

Or using reduce()

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)
}
1 Like

Or more concisely but more complex syntax

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

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