Say I have a following array which contains the following objects:
array = [{name: 'foo', number: 1}, {name: 'foo', number: 1}, {name: 'bar', number: 1}]
How would I go about finding the number of foo in this array? I am currently doing
search(name, array) {
let count = 0;
for (let i = 0; i < array.length; i++) {
if (array[i].name=== name) {
count++;
}
}
return count;
}
I would like to be able to run the search() function and be able to get the number of objects which has name foo and the number of objects which has name bar instead of having to pass in the name of the object itself.