0

この質問はよく出てきますが、私の特定の状況は、私がレビューした SO の回答ではカバーされていないようです。

削除したい重複配列を含む多次元配列があります。

[
    [8, 12],
    [8, 17],
    [8, 92],
    [8, 12]
]

[8, 12]二回登場。この重複を削除するにはどうすればよいですか?

4

1 に答える 1

2

What about the following:

var array1 = [[8,12],[8,17],[8,92],[8,12]];
var array2 = new Array();

for (var i=0; i<array1.length; i++) {
 var e1 = array1[i];
 var exists = false;
 for (var j=0; j<array2.length; j++) {
  if (array2[j][0] == e1[0] && array2[j][1] == e1[1]) exists = true;
 }
 if (exists == false) {
  array2[array2.length] = e1;
 }
}

the array2 is now array1 without duplicates. this should be way too slow for realtime game programming, but it should work. I am sorry, if I coded something wrong, it isn't intentional. I didn't test the code.

于 2013-09-16T16:56:51.247 に答える