Remove duplicate element pairs from multidimensional array

前端 未结 7 892
广开言路
广开言路 2020-12-11 07:23

I have an array that looks like this:

1.  coordinates = [ [16.343345, 35.123523],
2.                  [14.325423, 34.632723],
3.                  [15.231512,         


        
7条回答
  •  臣服心动
    2020-12-11 07:37

    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]);
    }
    

提交回复
热议问题