I have an array that looks like this:
我有一个看起来像这样的数组:
1. coordinates = [ [16.343345, 35.123523],
2. [14.325423, 34.632723],
3. [15.231512, 35.426914],
4. [16.343345, 35.123523],
5. [15.231512, 32.426914] ]
The latitude on line 5 is the same as on line 3, but they have different longitudes and are therefore not duplicates.
第5行的纬度与第3行的相同,但它们具有不同的经度,因此不是重复的。
Both the latitude and longitude are the same on line 3 and 6, and are therefore duplicates and one should be removed.
纬度和经度在第3行和第6行都是相同的,因此是重复的,应该删除一个。
7 个解决方案
#1
12
The difficulty in this question that different arrays never compare equal even if they contain same values. Therefore direct comparison methods, like indexOf
won't work.
这个问题的难度在于,即使它们包含相同的值,不同的数组也不会相等。因此,直接比较方法,如indexOf将无法正常工作。
The following pattern might be useful to solve this. Write a function (or use a built-in one) that converts arrays to scalar values and checks if these values are unique in a set.
以下模式可能有助于解决此问题。编写一个函数(或使用内置函数),将数组转换为标量值,并检查这些值在集合中是否唯一。
uniq = function(items, key) {
var set = {};
return items.filter(function(item) {
var k = key ? key.apply(item) : item;
return k in set ? false : set[k] = true;
})
}
where key
is a "hash" function that convert items
(whatever they are) to comparable scalar values. In your particular example, it seems to be enough just to apply Array.join
to arrays:
其中key是一个“哈希”函数,可以将项目(无论它们是什么)转换为可比较的标量值。在您的特定示例中,将Array.join应用于数组似乎已足够:
uniqueCoords = uniq(coordinates, [].join)
#2
5
You can use standard javascript function splice for this.
您可以使用标准的javascript函数拼接。
for(var i = 0; i < coordinates.length; i++) {
for(var j = i + 1; j < coordinates.length; ) {
if(coordinates[i][0] == coordinates[j][0] && coordinates[i][1] == coordinates[j][1])
// Found the same. Remove it.
coordinates.splice(j, 1);
else
// No match. Go ahead.
j++;
}
}
However, if you have thousands of points it will work slowly, than you need to consider to sort values at first, then remove duplicates in one loop.
但是,如果你有数千个点,它将比你需要考虑首先对值进行排序,然后在一个循环中删除重复项一样慢。
#3
1
I rewrote the answer from thg435 (It does not allow me to post comments) and prototype it also using jQuery instead, so this will work on all browsers using it (Even IE7)
我重写了thg435的答案(它不允许我发表评论)并使用jQuery改为原型,所以这适用于所有使用它的浏览器(即使是IE7)
Array.prototype.uniq = function (key) {
var set = {};
return $.grep(this, function (item) {
var k = key
? key.apply(item)
: item;
return k in set
? false
: set[k] = true;
});
}
You can use it like:
您可以像以下一样使用它:
arr = arr.uniq([].join);
#4
1
If you are not on Safari this single liner could do the job
如果您不在Safari上,这个单线程可以完成这项工作
var arr = [[16.343345, 35.123523],
[14.325423, 34.632723],
[15.231512, 35.426914],
[16.343345, 35.123523],
[15.231512, 32.426914]],
lut = {},
red = arr.filter(a => lut[a] ? false : lut[a] = true);
document.write("<pre>" + JSON.stringify(red,null,2) + "</pre>");
#5
0
It might be simpler to create another array keeping only unique coordinate pairs
创建另一个只保留唯一坐标对的数组可能更简单
var uniqueCoors = [];
var doneCoors = [];
for(var x = 0; x < coordinates.length; x++) {
var coorStr = coordinates[x].toString();
if(doneCoors.indexOf(coorStr) != -1) {
// coordinate already exist, ignore
continue;
}
doneCoors.push(coorStr);
uniqueCoors.push(coordinates[x]);
}
#6
0
function sortCoordinates(arr){
var obj = {};
for(var i = 0, l = arr.length; i < l; i++){
var el = arr[i];
var lat = el[0];
var lng = el[1];
if(!obj[lat + lng]){
obj[lat + lng] = [lat, lng];
}
}
var out = [];
for(p in obj){
out.push([obj[p][0], obj[p][1]]);
}
return out;
}
#7
-1
I am not sure about coordinates[][] dataType. Make the comparison accordingly.
我不确定coordinates [] [] dataType。进行相应的比较。
var dubJRows= new Array();
for(int i = 0; i < coordinates.length -2; i++){
for(int j = i+1; j < coordinates.length -1; j++){
if (i != j && chk_dubJRows_not_contains(j)) {
innerArray1 [1][1] = coordinates[i];
innerArray2 [1][1] = coordinates[j];
if ( innerArray1 [1][0] == innerArray2[1][0]
&& innerArray1[1][1] == innerArray2[1][1]) {
dubJRows.push(j);
}
}
}
}
//REMOVE ALL dubJRows from coordinates.
#1
12
The difficulty in this question that different arrays never compare equal even if they contain same values. Therefore direct comparison methods, like indexOf
won't work.
这个问题的难度在于,即使它们包含相同的值,不同的数组也不会相等。因此,直接比较方法,如indexOf将无法正常工作。
The following pattern might be useful to solve this. Write a function (or use a built-in one) that converts arrays to scalar values and checks if these values are unique in a set.
以下模式可能有助于解决此问题。编写一个函数(或使用内置函数),将数组转换为标量值,并检查这些值在集合中是否唯一。
uniq = function(items, key) {
var set = {};
return items.filter(function(item) {
var k = key ? key.apply(item) : item;
return k in set ? false : set[k] = true;
})
}
where key
is a "hash" function that convert items
(whatever they are) to comparable scalar values. In your particular example, it seems to be enough just to apply Array.join
to arrays:
其中key是一个“哈希”函数,可以将项目(无论它们是什么)转换为可比较的标量值。在您的特定示例中,将Array.join应用于数组似乎已足够:
uniqueCoords = uniq(coordinates, [].join)
#2
5
You can use standard javascript function splice for this.
您可以使用标准的javascript函数拼接。
for(var i = 0; i < coordinates.length; i++) {
for(var j = i + 1; j < coordinates.length; ) {
if(coordinates[i][0] == coordinates[j][0] && coordinates[i][1] == coordinates[j][1])
// Found the same. Remove it.
coordinates.splice(j, 1);
else
// No match. Go ahead.
j++;
}
}
However, if you have thousands of points it will work slowly, than you need to consider to sort values at first, then remove duplicates in one loop.
但是,如果你有数千个点,它将比你需要考虑首先对值进行排序,然后在一个循环中删除重复项一样慢。
#3
1
I rewrote the answer from thg435 (It does not allow me to post comments) and prototype it also using jQuery instead, so this will work on all browsers using it (Even IE7)
我重写了thg435的答案(它不允许我发表评论)并使用jQuery改为原型,所以这适用于所有使用它的浏览器(即使是IE7)
Array.prototype.uniq = function (key) {
var set = {};
return $.grep(this, function (item) {
var k = key
? key.apply(item)
: item;
return k in set
? false
: set[k] = true;
});
}
You can use it like:
您可以像以下一样使用它:
arr = arr.uniq([].join);
#4
1
If you are not on Safari this single liner could do the job
如果您不在Safari上,这个单线程可以完成这项工作
var arr = [[16.343345, 35.123523],
[14.325423, 34.632723],
[15.231512, 35.426914],
[16.343345, 35.123523],
[15.231512, 32.426914]],
lut = {},
red = arr.filter(a => lut[a] ? false : lut[a] = true);
document.write("<pre>" + JSON.stringify(red,null,2) + "</pre>");
#5
0
It might be simpler to create another array keeping only unique coordinate pairs
创建另一个只保留唯一坐标对的数组可能更简单
var uniqueCoors = [];
var doneCoors = [];
for(var x = 0; x < coordinates.length; x++) {
var coorStr = coordinates[x].toString();
if(doneCoors.indexOf(coorStr) != -1) {
// coordinate already exist, ignore
continue;
}
doneCoors.push(coorStr);
uniqueCoors.push(coordinates[x]);
}
#6
0
function sortCoordinates(arr){
var obj = {};
for(var i = 0, l = arr.length; i < l; i++){
var el = arr[i];
var lat = el[0];
var lng = el[1];
if(!obj[lat + lng]){
obj[lat + lng] = [lat, lng];
}
}
var out = [];
for(p in obj){
out.push([obj[p][0], obj[p][1]]);
}
return out;
}
#7
-1
I am not sure about coordinates[][] dataType. Make the comparison accordingly.
我不确定coordinates [] [] dataType。进行相应的比较。
var dubJRows= new Array();
for(int i = 0; i < coordinates.length -2; i++){
for(int j = i+1; j < coordinates.length -1; j++){
if (i != j && chk_dubJRows_not_contains(j)) {
innerArray1 [1][1] = coordinates[i];
innerArray2 [1][1] = coordinates[j];
if ( innerArray1 [1][0] == innerArray2[1][0]
&& innerArray1[1][1] == innerArray2[1][1]) {
dubJRows.push(j);
}
}
}
}
//REMOVE ALL dubJRows from coordinates.