0

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;
Kubadev
  • 867
  • 10
  • 25
  • 2
    Possible duplicate of [Counting the occurrences / frequency of array elements](https://stackoverflow.com/questions/5667888/counting-the-occurrences-frequency-of-array-elements) – VLAZ Aug 01 '19 at 10:35
  • @Rajesh I wouldn't have marked it if it didn't show up within 10 seconds of searching from within the dupe vote screen itself. I just did `array count [javascript]` and it was the second item. If I didn't get a result (at least one that didn't require scrolling), I'd have left it, honestly. – VLAZ Aug 01 '19 at 10:38
  • I'm completely new to Javascript buddys... @Rajesh it's not kind. And if i post here, it's because i don't know what to search for. – Kubadev Aug 01 '19 at 10:38
  • @KubNetwork I understand that. If you were expert, you wont post question in the first place. But the point is, you should put some effort. Even if you search your title, you will get some help. Then its a matter of connecting the dots. And effort does not mean just code, you can even share your search queries/ any reference link you found but didn't help – Rajesh Aug 01 '19 at 10:41
  • 1
    @KubNetwork Please refer to my last comment under *adiga*'s comment. That should be all you needed to solve on your own. Also, a user with your rep should know importance of effort. We are here to help and not do others' work. Hope to see you again but with me helping! – Rajesh Aug 01 '19 at 10:48

4 Answers4

4

I'm assuming Fruit_obj is the name of class or constrcutor function. You could use instanceof to filter the array:

const fruits = array.filter(o => o instanceof Fruit_obj)

This returns all the objects of type Fruit_obj. You can get the count using fruits.length

adiga
  • 34,372
  • 9
  • 61
  • 83
  • @Rajesh this is not that trivial or obvious to google tbh. `Fruit_obj` is not a property – adiga Aug 01 '19 at 10:38
  • @adiga Thans adiga, it's exactly what i was looking for :) – Kubadev Aug 01 '19 at 10:43
  • True. The problem has 2 parts. 1. How to filter objects and count them. 2. How to check if object is of a specific constructor. My POV is, if OP shares some effort, google search query, reference links they followed, code, we know their level and draft answer in that way. Just solving the problem is wrong as that does not solve the real problem – Rajesh Aug 01 '19 at 10:43
  • 1
    @Rajesh See, your explanation is really helpfull Rajesh. I do not know that i needed to focus on "How to check if object is of a specific constructor". I was in lack of the correct term to answer my question AND to trying to find solution before posting. So thank you, and please be a little more kind with newby... Have a good day – Kubadev Aug 01 '19 at 10:50
2

With the help of filter() and hasOwnProperty() you can get the count

const array = [{'Fruit_obj': 1}, {'name': 1}, {'Fruit_obj': 1}, {'name': 1}, {'Fruit_obj': 1}];
const length = array.filter(obj => obj.hasOwnProperty('Fruit_obj')).length
console.log(length)
MH2K9
  • 11,951
  • 7
  • 32
  • 49
1

You can simply do something like this to get the number of objects with the key Fruit_obj

array.filter(obj => obj['Fruit_obj']).length
0

You can use .hasOwnProperty() to search for the property Fruit_obj in the object

ellipsis
  • 12,049
  • 2
  • 17
  • 33
  • `.hasOwnProperty` is used to filter via a property value of my object. What i needed, is to filter my array with the className. I'll use `instanceof` – Kubadev Aug 02 '19 at 14:04