title: Use the first and last element (dates) in an array to create an obj.name in an Angular factory/service
body: <p>I have an Angular service/factory method that takes as a parameter passed to it in the form of an array of arrays containing objects <code>[[{},{}],[{},{}]]</code>.</p>
  
  <p>The data contained in this parameter, consist of arrays of weeks, with an object for each day consisting of a date and an integer value. Ex. <code>{"2017-01-10": 711}</code>. </p>
  
  <p>The method's function is to group each array of weeks in a single object, with its integer values totaled. Ex. <code>{"name": "Week 1", "total": 3228}</code></p>
  
  <p><strong>How can I take the first and last element of each array, and create a name/label from this?</strong> So, the output would look like this: <code>{"name": "Week 1 - 2017-01-10 to 2017-01-15", "total": 3228}</code></p>
  
  <p><strong>Here is an example of the data passed to the method:</strong></p>
  
  <pre><code>[
     [
        {
           "2016-11-01": 319
        },
        {
           "2016-11-02": 782
        },
        {
           "2016-11-03": 579
        },
        {
           "2016-11-04": 642
        },
        {
           "2016-11-05": 444
        },
        {
           "2016-11-06": 274
        }
     ],
     [
        {
           "2016-11-07": 319
        },
        {
           "2016-11-08": 782
        },
        {
           "2016-11-09": 579
        },
        {
           "2016-11-10": 642
        },
        {
           "2016-11-11": 444
        },
        {
           "2016-11-12": 274
        },
        {
           "2016-11-13": 481
        }
     ]
  ]
  </code></pre>
  
  <p><strong>Here is the method:</strong></p>
  
  <pre><code>function assignWeekNamesAndTotals(arrayOfWeeks) {
  
      var data = arrayOfWeeks;
  
      var result = data.reduce(function (p, c, i) {
  
          var total = c.reduce(function (total, obj) {
              for (var k in obj) {
                  total += obj[k]; // calculate total
              }
              return total;
          }, 0);
  
          // Format object in any format you want
          var tmp = {};
          tmp.name = "Week " + (i + 1)
          tmp.total = total;
  
          p.push(tmp)
          return p;
      }, [])
  
      console.log("Assign Week names and Totals Output (Step 2: " + JSON.stringify(result, null, "   "));
      return result;   
  }
  </code></pre>
  
  <p>I appreciate your guidance!</p>