I have a parent class called Tree and i build objects from children contructor Fruit_obj and Book_obj stored in Tree.m_objects. This is my array :
Tree.m_objects = [{Fruit_obj}, {Fruit_obj}, {Fruit_obj}, {Book_obj}, {Book_obj}];
When i console.log(Tree.m_objects) in DevTools, it shows :
► O : Fruit_obj {Some members}
► 1 : Fruit_obj {Some members}
► 2 : Fruit_obj {Some members}
► 3 : Book_obj {Some members}
► 4 : Book_obj {Some members}
What i want to do, is to filter my array to count how many occurrence of Fruit_obj
i have in my array. But i only want to filter them by their className (here, Fruit_obj
).
[Edit: Thanks to Adiga, i found that i need to use instanceOf
to find my class name]
const fruits = array.filter(o => o instanceof Fruit_obj);
return fruits.length;