title: SAPUI5 Javascript - Get first and last elements of array for each unique property
body: <p>SAPUI5 - I have an array of objects and one of the properties in those is 'Category'.</p>
  
  <p>For example say I have 2 different types of Category, 'Front Shop' and 'Production Area', what I need to do is to be able to get the first value of each and the last value of each, and then set the enabled property of a button as enabled/disabled.</p>
  
  <p>I'm currently using undercore js (_.each) to loop through to perform some other logic, so can include additional logic here.</p>
  
  <p>Not sure if Underscore has a built in function for this? 
  Or could someone point me in the right direction on how to do this?</p>
  
  <p>I've got my first pass at what was wanted where I get the very first result and the last result, but now need to set this for each unique category.</p>
  
  <p><a href="https://i.stack.imgur.com/eDUgP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eDUgP.png" alt="enter image description here"></a></p>
  
  <p>Example code below:</p>
  
  <pre><code>// Set view data
                  oViewData.Questions = oQuestions.results;
                  oViewData.Questions.TotalNumberOfQuestions = oQuestions.results.length;
  
                  // Loop Questions, to get Category Desc and Competency Desc values from relevant Sets
                  _.each(oViewData.Questions, function (result, index) {
  
                      // Read and set Category Desc
                      this.getView().getModel("Survey").read("/CategorySet", {
                          filters: [new Filter("CategoryId", FilterOperator.EQ, result.CategoryId)],
                          success: function (oData) {
                              oViewData.Questions[index]._CategoryDesc = oData.results[0].CategoryDesc;
                              this.setViewData(oViewData);
                          }.bind(this),
                          error: function (oError) {}.bind(this)
                      });
  
                      // Read and set Competency Desc
                      this.getView().getModel("Survey").read("/CompetencySet", {
                          filters: [new Filter("CompetencyId", FilterOperator.EQ, result.CompetencyId)],
                          success: function (oData) {
                              oViewData.Questions[index]._CompetencyDesc = oData.results[0].CompetencyDesc;
                              this.setViewData(oViewData);
                          }.bind(this),
                          error: function (oError) {}.bind(this)
                      });
  
                      // Set all move up / down buttons to enabled
                      oViewData.Questions[index]._MoveUpBtn = true;
                      oViewData.Questions[index]._MoveDownBtn = true;
  
                      // if category id is the first one in the list 
  
  
  
                  }.bind(this));
  
                  // Overwrite first move up button and last move down btn to disabled
                  oViewData.Questions[0]._MoveUpBtn = false;
                  oViewData.Questions.slice(-1)[0]._MoveDownBtn = false;
  
                  // Set view data
                  this.setViewData(oViewData);
  </code></pre>