title: Sort and remove duplicate items from array - REST API SharePoint lists
body: <p>I have successfully collated the results from multiple lists from SharePoint Online in a grid using REST API.</p>
  
  <p>However, the collated resultset array is not in order and has duplicate elements. I need to order the grid, so that it is sorted based on created date desc and only has unique items. </p>
  
  <p>I used the below code and RemoveDuplicatesItems<a href="https://i.stack.imgur.com/6LqEr.png" rel="nofollow noreferrer">enter image description here</a> function. The code executes fine and removes duplicates, but the grid shows all columns as undefined once i remove duplicates. If i don't execute the duplicate function , grid displays data, but with duplicates.</p>
  
  <p>Any technical advice would be much helpful to me. Looking forward to hear from you. Thanks all. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://dev.sharepoint.com/sites/dev/SiteAssets/jquery-1.10.2.js"&gt;&lt;/script&gt;
  &lt;script src="https://dev.sharepoint.com/sites/dev/SiteAssets/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  var ListDataCollection = {
      Lists:[{
          Url:"https://dev.sharepoint.com/sites/dev/sites1/_api/web/lists/getbytitle('Employee')/items?$top=5&amp;$orderby=Created desc&amp;$select=Title,Description,Action_x0020_By_x003a_,RequestFrom_x003a_,Created&amp;$filter=Category eq 'Key Events and Decision'",
          Completed:false
      },{
          Url:"https://dev.sharepoint.com/sites/dev/sites2/_api/web/lists/getbytitle('Employee')/items?$top=5&amp;$orderby=Created desc&amp;$select=Title,Description,Action_x0020_By_x003a_,RequestFrom_x003a_,Created&amp;$filter=Category eq 'Key Events and Decision'",
          Completed:false
      },{
          Url:"https://dev.sharepoint.com/sites/dev/sites3/_api/web/lists/getbytitle('Employee')/items?$top=5&amp;$orderby=Created desc&amp;$select=Title,Description,Action_x0020_By_x003a_,RequestFrom_x003a_,Created&amp;$filter=Category eq 'Key Events and Decision'",
          Completed:false
      }]
  };
  var tableContent = '&lt;table id="tableEmployee" style="width:100%" border="1 px"&gt;&lt;thead&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;No&lt;/b&gt;&lt;/td&gt;' + 
  
  '&lt;td&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/td&gt;' + '&lt;td&gt;&lt;b&gt;Action By&lt;/b&gt;&lt;/td&gt;' + '&lt;td&gt;&lt;b&gt;Request From&lt;/b&gt;&lt;/td&gt;' + '&lt;td&gt;&lt;b&gt;Created&lt;/b&gt;&lt;/td&gt;'+ 
  
  '&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;';
  
  function groupBy(items,propertyName)
  {
      var result = [];
      $.each(items, function(index, item) {
         if ($.inArray(item[propertyName], result)==-1) {
            result.push(item[propertyName]);
         }
      });
      return result;
  }
  function RemoveDuplicateItems(items, propertyName) {
      var result = [];
          $.each(items, function (index, item) {
              if ($.inArray(item[propertyName], result) == -1) {
                  result.push(item[propertyName]);
              }
          });
      return result;
  }
  function ifAllCompleted() {
      for (var i=0;i&lt;ListDataCollection.Lists.length;i++) {
       
          if (!ListDataCollection.Lists[i].Completed) {
              return false;
          }   
      }
   console.log('Completed');   
      var arrayOfAllData = ListDataCollection.Lists[0].Data.d.results;
      arrayOfAllData = arrayOfAllData.concat(ListDataCollection.Lists[1].Data.d.results);
      arrayOfAllData = arrayOfAllData.concat(ListDataCollection.Lists[2].Data.d.results);
   console.log('Total elements : ' + arrayOfAllData.length);
   arrayOfAllData=RemoveDuplicateItems(arrayOfAllData,'Description')
   for( var i=0; i&lt;arrayOfAllData.length;i++)
   {
       tableContent += '&lt;tr&gt;';
                      tableContent += '&lt;td&gt;' + arrayOfAllData[i].Title+ '&lt;/td&gt;';
                      tableContent += '&lt;td&gt;' + arrayOfAllData[i].Description+ '&lt;/td&gt;';
                      tableContent += '&lt;td&gt;' + arrayOfAllData[i].Action_x0020_By_x003a_+ '&lt;/td&gt;';
                      tableContent += '&lt;td&gt;' + arrayOfAllData[i].RequestFrom_x003a_+ '&lt;/td&gt;';
       tableContent += '&lt;td&gt;' + arrayOfAllData[i].Created+ '&lt;/td&gt;';
                      tableContent += '&lt;/tr&gt;';
   }
   $('#employeeGrid').append(tableContent);
   
   
  }
  
  $(document).ready(function(){
      for (var x=0;x&lt;ListDataCollection.Lists.length;x++) {
          $.ajax({
              url:ListDataCollection.Lists[x].Url,
              indexValue:x,
              method: "GET",
              headers: { "Accept": "application/json; odata=verbose" },
              success: function (data, status, xhr) {
                  ListDataCollection.Lists[this.indexValue].Data = data;
                  ListDataCollection.Lists[this.indexValue].Completed = true;
                  ifAllCompleted();
              },
              error: function (xhr, status, error) {
                  console.log('error');
              }
          });     
      }
  });
  &lt;/script&gt;
   &lt;div id="CustomerPanel"&gt;
          &lt;table id="tableEmployee" border="1 px" style="width: 100%;"&gt;
              &lt;tbody&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;div id="employeeGrid" style="width: 100%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
      &lt;/div&gt; </code></pre>
  </div>
  </div>
  </p>