Using underscore , you can count the number of occurrences of each element using this code:
_.countBy(_.flatten(array), _.identity)
Thus, the function foo can be implemented as follows:
function bar(foo, element){ return _.countBy(_.flatten(foo), _.identity)[element]; } var foo = ["a", ["b", "c", "d"], "a"] console.log(bar(foo, "a"));
Although this solution is not recursive, I should mention.
source share