Remove duplicate element pairs from multidimensional array

前端 未结 7 898
广开言路
广开言路 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:29

    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)

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

提交回复
热议问题