2

我有一個網頁,它使用jQuery從包含位置名稱,緯度和經度的JSON文件中獲取數據。然後我將檢索到的數據推送到一個數組中。使用Javascript,jQuery和underscore.js刪除多維數組中的重複項目

var weatherSites = []; 
function weather() { 
    $.getJSON('json/LOCS.json', function(data) { 
     $.each(data.locations, function() { 
     var locationLatitude = this.latitude; 
     var locationLongitude = this.longitude; 
     var locationName = this.location; 
     // -- If the latitude and longitude are 0,0 then skip to the next iteration -- 
      if (locationtLatitude == +0 || locationLatitude == -0 && locationLongitude == +0 || locationLongitude == -0) { 
       return true; 
      } 

     // Populate array with site name, latitude, and longitude  
     weatherSites.push({name: locationName, latitude: locationLatitude, longitude: locationLongitude}); 

    }); 
    }); 

    }; 

該代碼檢索數據並根據需要填充數組。然而,儘管​​和/或locationLongitude是不同的,但是有多個locationName項目是相同的。無論經緯度如何,我需要從數組元素中刪除locationName是相同的。

我試過用這裏看到的很多方法,但都沒有運氣。例如:

function removeDupes() { 
     var uniques = _.map(_.groupBy(weatherSites,function(doc){ 
      return doc.id; 
     }),function(grouped){ 
      return grouped[0]; 
     }); 

此示例需要underscore.js。這對我不起作用。一般來說,我是編程和GIS的新手。我將不勝感激一個答案,其中包含所需的完整代碼,並希望發生的事情背後的邏輯。這對我很有幫助,當然會幫助我學習。

謝謝你的幫助。

+0

這將是有益的:http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – KRR 2015-03-31 21:18:30

回答

0

開源項目http://www.jinqJs.com可以輕鬆做到。 這裏是GitHub的鏈接:GitHub

var results = jinqJs() 
        .from(data1) 
        .groupBy('locationName') 
        .max('locationLatitude', 'locationLongitude') 
        .select(); 

讓我知道如果你需要任何工作的例子。