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(); 

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