使用 Javascript、jQuery 和 underscore.js 从多维数组中删除重复项
Remove Duplicate Items From Multidimensional Array Using Javascript, jQuery, & underscore.js
我有一个网页使用 jQuery 从包含位置名称、纬度和经度的 JSON 文件中获取数据。然后我将检索到的数据推送到一个数组中。
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});
});
});
};
代码检索数据并根据需要填充数组。但是,尽管 locationLatitude
and/or locationLongitude
不同,但仍有许多 locationName
项相同。我需要从数组中删除 locationName
相同的元素,无论纬度或经度如何。
我已经尝试使用此处看到的多种方法,但都没有成功。例如:
function removeDupes() {
var uniques = _.map(_.groupBy(weatherSites,function(doc){
return doc.id;
}),function(grouped){
return grouped[0];
});
此示例需要 underscore.js。这对我不起作用。一般来说,我是编程和 GIS 的新手。我将不胜感激,其中包含必要的完整代码,并希望包含正在发生的事情背后的逻辑。这对我很有帮助,当然会帮助我学习。
感谢您的帮助。
开源项目http://www.jinqJs.com可以轻松做到。
这是 GitHub link: GitHub
var results = jinqJs()
.from(data1)
.groupBy('locationName')
.max('locationLatitude', 'locationLongitude')
.select();
如果您需要任何工作示例,请告诉我。
我有一个网页使用 jQuery 从包含位置名称、纬度和经度的 JSON 文件中获取数据。然后我将检索到的数据推送到一个数组中。
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});
});
});
};
代码检索数据并根据需要填充数组。但是,尽管 locationLatitude
and/or locationLongitude
不同,但仍有许多 locationName
项相同。我需要从数组中删除 locationName
相同的元素,无论纬度或经度如何。
我已经尝试使用此处看到的多种方法,但都没有成功。例如:
function removeDupes() {
var uniques = _.map(_.groupBy(weatherSites,function(doc){
return doc.id;
}),function(grouped){
return grouped[0];
});
此示例需要 underscore.js。这对我不起作用。一般来说,我是编程和 GIS 的新手。我将不胜感激,其中包含必要的完整代码,并希望包含正在发生的事情背后的逻辑。这对我很有帮助,当然会帮助我学习。
感谢您的帮助。
开源项目http://www.jinqJs.com可以轻松做到。 这是 GitHub link: GitHub
var results = jinqJs()
.from(data1)
.groupBy('locationName')
.max('locationLatitude', 'locationLongitude')
.select();
如果您需要任何工作示例,请告诉我。