count amount of filled local variables

Hi,

i’ve been searching for an easy way to count the amount of filled variables.

This is what I came across, but this is for empty variables. I think I need the opposite and it has to work in Workflow with the Run Script plugin.

var arr = [1,,2,5,6,,4,5,6,,], count = 0, i = arr.length;

while (i--) {
    if (typeof arr[i] === "undefined")
        count++;
}

javascript - count empty values in array - Stack Overflow

thanks in advance,

Koen

If the array values are always numeric (or empty) then the following would work:

var arr = [1,,2,5,6,,4,5,6,,], i = arr.length,count = i;
while (i--) {
    if (isNaN(arr[i])) count--;
}
Watch.Log(count,2);

Hi Phil,

the value is default 0 or > 0. Only the value > 0 has to be counted.

you scripts works, only it counts all variables now.

What do I need to change to get only result that are greater than 0?

Actually, my script counted all numerical values, regardless of their actual value. To only count positive, non-zero numbers, you’ll have to amend the code this way:

var arr = [1,0,2,5,6,-1,4,5,6,,0,12], i = arr.length,count = i;
while (i--) {
    if (isNaN(arr[i])||arr[i]<=0) count--;
}
Watch.Log(count,2);

The result will yield 8 because out of the 12 values, 1 is undefined, 2 are equal to zero and 1 is negative.

thanks! that helped a lot. :slight_smile: