Skip to content
Advertisement

Count occurrences of a specific value in multidimensional array

Let’s say I have a multidimensional array like this:

JavaScript

How would I be able to count how many times the value “Thing1” occurs in the multidimensional array?

Advertisement

Answer

Try this :

JavaScript

Output :

JavaScript

It gives the occurrence of each value. ie : Thing1 occurs 2 times.

EDIT : As per OP’s comment : “Which array do you mean resulting array?” – The input array. So for example this would be the input array: array(array(1,1),array(2,1),array(3,2)) , I only want it to count the first values (1,2,3) not the second values (1,1,2) – gdscei 7 mins ago

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement