title: JavaScript destructure object into other object set default value
body: <p>I am destructuring the value of an object from an array. If array is empty, error happens. How to make default value, if array is empty?</p>
  <p>Error happens if array, which I am destructuring is empty:</p>
  <blockquote>
  <p>&quot;TypeError&quot;,&quot;message&quot;:&quot;Cannot destructure property <code>payments</code> of 'undefined' or 'null'.&quot;</p>
  </blockquote>
  <p>Code of destructuring (<code>await (...).toArray()</code> is returning array <code>[ { payments: @integer } ]</code>):</p>
  <pre><code>    ({
          [0]: { payments: users.finances.payments = 0 },
      } = await (
          await payments_collection.aggregate([
              {
                  $group: {
                      _id: null,
                      payments: { $sum: '$coins' },
                  },
              },
          ])
      ).toArray());
  </code></pre>