Python to JS translation: list.count(object)

Hi,
Just wondering if anyone knows the Javascript translation of python’s .count function?

In my experiment I have a list of objects which gets added to as the participant selects objects and then I count how many times each object has been added to the list which determines the stimuli I show next.

In Python this work well and the code is:

Begin Experiment:
FriendsList = [ ]

Begin Routine:
if SearchMouse.clicked_name[0] == CorrectArrow:
FriendsList.append(Who)

End Routine:
if FriendsList.count(‘Rabbit’) > 0:
RabOpacity = 1
else:
RabOpacity = 0

However this doesnt translate into JS very well, from reading the crib sheet I have managed to change the .append to .push but I am now stuck on how to translate the .count

Any help would be much appreciated
Elle :slight_smile:

I have manged to solve my issue!
Posting the solution incase anyone else encounters this problem:

Using list.includes(object) in JS I can get it to search for whether my list contains the object.

Python code: FriendsList.append(Who) --> JS code: FriendsList.push(Who)
Python code: if FriendsList.count(‘Rabbit’) > 0: --> JS code: if FriendsList.includes(‘Rabbit’) == true

I know this isnt doing exactly the same thing, but it works just fine for what I need.

1 Like