title: JavaScript ES6 - count duplicates to an Array of objects
body: <p>I'm creating for my list of products a filter to count all producers and display like this:</p>
  
  <p><strong>Apple (3)</strong></p>
  
  <p>I eliminated the duplicates from array: ["Apple","Apple","Apple"] I used this link:</p>
  
  <p><a href="https://stackoverflow.com/questions/840781/get-all-non-unique-values-i-e-duplicate-more-than-one-occurrence-in-an-array">Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array</a></p>
  
  <p>But my problem is that I want to count these elements from array and display them in an Array of Objects cause i need to iterate it later.</p>
  
  <p>From this Array of Apples above i need result: [{"Apple": 3},{...},{...}] </p>
  
  <p>I was trying to do this but it returns me <strong>object</strong> and I can't iterate after it:
  <a href="https://stackoverflow.com/questions/19395257/how-to-count-duplicate-value-in-an-array-in-javascript">How to count duplicate value in an array in javascript</a></p>
  
  <p><strong>I need an Array of Objects</strong> it's not duplicated</p>
  
  <p>I'm using Angular 4.</p>
  
  <p>My code:</p>
  
  <p><strong>component.ts</strong></p>
  
  <pre><code>  async ngOnInit() {
      this.cart$ = await this.cartService.getCart();
  
      this.subscription = this.productService.getAll().subscribe(products =&gt; {
        this.category = products.filter(
          products =&gt; products.category == this.name
        );
        this.filters();
      });
    }
  
    filters() {
      this.category2 = this.category.map(value =&gt; value.producer);
      this.filteredArray = this.eliminateDuplicates(this.category2);
      console.log(this.filteredArray);
    }
  
    eliminateDuplicates(arr) {
      let i,
        len = arr.length,
        out = [],
        obj = {};
  
      for (i = 0; i &lt; len; i++) {
        obj[arr[i]] = 0;
      }
      for (i in obj) {
        out.push(i);
      }
      return out;
    }
  </code></pre>
  
  <p><strong>component.html</strong></p>
  
  <pre><code>   &lt;div *ngFor="let f of filteredArray"&gt;
        {{f}}
     &lt;/div&gt;
  </code></pre>