title: Filter array by criteria and by object property type
body: <p>I have an array of objects and I want to return only the first 2 results for each type in the object. The type is variable. I don't know exactly how many types I will have either.</p>
  <p>For instance, I have this array:</p>
  <pre><code>var obj = [
  {
      &quot;number&quot;: 1,
      &quot;type&quot;: &quot;one&quot;
  }, {
      &quot;number&quot;: 2,
      &quot;type&quot;: &quot;one&quot;
  }, {
      &quot;number&quot;: 3,
      &quot;type&quot;: &quot;one&quot;
  }, {
      &quot;number&quot;: 4,
      &quot;type&quot;: &quot;one&quot;
  }, {
      &quot;number&quot;: 5,
      &quot;type&quot;: &quot;one&quot;
  }, {
      &quot;number&quot;: 1,
      &quot;type&quot;: &quot;two&quot;
  }, {
      &quot;number&quot;: 2,
      &quot;type&quot;: &quot;two&quot;
  }, {
      &quot;number&quot;: 3,
      &quot;type&quot;: &quot;two&quot;
  }, {
      &quot;number&quot;: 4,
      &quot;type&quot;: &quot;two&quot;
  }, {
      &quot;number&quot;: 5,
      &quot;type&quot;: &quot;two&quot;
  }, {
      &quot;number&quot;: 6,
      &quot;type&quot;: &quot;two&quot;
  }
  ];
  </code></pre>
  <p>In this example we have type: one and type: two as a simple example. The types could be anything and the number could be many.</p>
  <p>I want this to be returned:</p>
  <pre><code>var obj = [
  {
      &quot;number&quot;: 1,
      &quot;type&quot;: &quot;one&quot;
  }, {
      &quot;number&quot;: 2,
      &quot;type&quot;: &quot;one&quot;
  } {
      &quot;number&quot;: 1,
      &quot;type&quot;: &quot;two&quot;
  }, {
      &quot;number&quot;: 2,
      &quot;type&quot;: &quot;two&quot; // &lt;-- Could be several more types
  }
  ];
  </code></pre>
  <p>I know I can use filter to get the results over the entire array, but I want it done for each variable type.</p>
  <pre><code>var result = obj.filter((val,i)=&gt;i&lt;3);
  </code></pre>