title: Need help to optimize handling data from Query
body: <p>So I'm trying to get the data from my Database to export to a third-party source. I have all my items separate for specific reasons on how I process my data.</p>
  
  <p>Though they are similar but not the same. Though the third-party source doesn't care if they are at least similar in one way which is their SKU.</p>
  
  <p>So I need to condense my data to count all items with the same SKU. So make the array smaller but now it has quantity added.
   so like <code>[{SKU:1234},{SKU:1234},{SKU:1234},{SKU:1234}]</code> TO <code>[{SKU:1234, quantity:4}]</code> </p>
  
  <p>The way I do it is quite slow and can't think of a better way to do it off the top of my head v.v</p>
  
  <pre><code>router.get("/export", Auth, async (req, res) =&gt; {
    let AllProducts = await Products.find({
      store: req.decoded.user.storeid,
      shipped: false,
      pullsheet: { $ne: true }
    }).sort("sku");
  
  
    let dupArray = [];
    let csvArray = [];
  
    for (let i = 0; i &lt; AllProducts.length; i++) {
      let noMatch = true;
      for (let d = 0; d &lt; dupArray.length; d++) {
        if (AllProducts[i].sku === dupArray[d].sku) {
          if (dupArray[d].quantity) {
            dupArray[d].quantity = dupArray[d].quantity + 1;
          }
          noMatch = false;
          console.log(dupArray[d]);
          let removed = AllProducts.splice(i, 1);
          i--;
          d = dupArray.length;
        }
      }
      if (noMatch) {
        let removed = AllProducts.splice(i, 1);
        removed[0].quantity = 1;
        dupArray.push(removed[0]);
        i--;
      }
    }
  
    res.send(dupArray);
  });
  </code></pre>