Count equal element in array

Hello everyone !

I need help.

I have boolean field "sendSMS" with many values, for exemple :
sendSMS : [true , false, true]

and i want use scripted field to count the number of "true" in my field.

Thanks in advance for your future answers.

Painless supports for loop, so you can easily iterate over your array of values. You would need to do something like:

int count = 0;
for (val in doc['sendSMS']) {
  if (val == true) {
    count++;
  }
}
emit(count);
1 Like

Thanks for your answer, but when i try your script, i have an error : "reason": "Unknown call [emit] with [1] arguments."

But I can't solve this error...

Do you have an idea please?

Have you tried return?

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