1. 程式人生 > >JS:利用for循環進行數組去重

JS:利用for循環進行數組去重

scrip document 示例 nbsp stat for循環 進行 bre script

<script>

 var a = [1, 3, 2, 4, 5, 3, 2, 1, 4, 6, 7, 7, 6, 6];

//示例數組
var b = [];

for(var i = 0; i < a.length; i++) {
var status = 0;
for(var j = 0; j < b.length; j++) {
if(a[i] == b[j]) {
status = 1;
break;
}
}
if(status == 0) {
b.push(a[i]);
}
}
for(var x in b) {
document.write(b[x] + "&nbsp;");
}
</script>

JS:利用for循環進行數組去重