【发布时间】:2018-02-23 16:57:39
【问题描述】:
我希望有人能解释我的 for 循环有什么问题。如果我将 for 循环输入为 if(arguments[2]){ stable = rere(stable, arguments[2]);} 它会按预期工作。我可以有超过 6 个参数,并且想了解为什么这个循环不能正常运行。
function sym() {
function rere(a,b){
var check = [];
for(i=a.length-1; i>=0; i--){
for(x=b.length-1; x>=0; x--){
if(a[i] == b[x]){
check.push( a.splice(i,1) );
}
}
}
for(i=b.length-1; i>=0; i--){
for(x=check.length-1; x>=0; x--){
if(b[i] == check[x][0]){
b.splice(i,1);
}
}
}
var stable = a.concat(b);
return stable;
}
var stable = rere(arguments[0], arguments[1]);
//problem is HERE. The for loop repeating rere function crashes repl.it
for(i=2; i<arguments.length; i++){
stable = rere(stable, arguments[i]);
}
//End problem.
stable = stable.filter(function(a,b,c){
return b == c.indexOf(a);
});
return stable;
}
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]);
【问题讨论】:
-
错误是什么?
-
所以你想过滤掉重复项?
标签: javascript for-loop arguments