Javascript array.prototype几乎到处都在打印
由于我必须从数组中删除一些元素,因此我遵循stackoverflow中的几段代码,得出了以下结论:Javascript array.prototype几乎到处都在打印,javascript,jquery,arrays,Javascript,Jquery,Arrays,由于我必须从数组中删除一些元素,因此我遵循stackoverflow中的几段代码,得出了以下结论: Array.prototype.remove = function(from, to) { var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; return this.push.apply(this, rest); }
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
然而,出于某些原因,每当有东西与数组有关时,就会到处打印这段代码
例如:
这段代码:
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
Scadenza.prototype.init = function() {
this.promemoria = (this.promemoria == "")?("NO"):(this.promemoria);
var gruppo = this.group; // convert array to string.
this.group = "";
for (var i in gruppo) {
if (i != (gruppo.length - 1)) {
this.group += gruppo[i] + ", ";
}
else {
this.group += gruppo[i];
}
}
alert(this.group);
};
我认为,这段代码应该将临时存储在变量gruppo中的数组This.group转换成一个非常明显的字符串
当然,如果不是因为它的警报是:
[DATA Required]函数from,to{var rest=this.sliceto | | from
+1 | | this.length;this.length=from<0?this.length+from:from;返回this.push.applythis,rest;}
这段代码也通过AJAX请求发送到数据库,查询的结果(在所需列中)如下:
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
函数from,to{var rest=this.sliceto | | from+1||
this.length;this.length=from<0?this.length+from:from;
返回this.push.applythis,rest;}
我很惊讶这会发生,但我完全不知道如何修复它
加载页面或单击引发此事件的按钮时,未收到任何错误警报
有什么想法吗
ps:不确定是否有帮助,但我正在使用jQuery
@评论:
正常的for循环实际上无法解决此问题:
Scadenza.prototype.init = function() {
this.promemoria = (this.promemoria == "")?("NO"):(this.promemoria);
var gruppo = this.group; // convert array to string.
this.group = "";
for (var i = 0; i < gruppo.length; i++) {
if (i != (gruppo.length - 1)) {
this.group += gruppo[i] + ", ";
}
else {
this.group += gruppo[i];
}
}
alert(this.group);
};
警报仍然是相同的。对var i=0使用适当的;i、 不要在阵列上使用它们。例如,在init方法中就是这样做的
顺便说一句,对于您无论如何都要使用的任务:
更改阵列原型时,该方法将作为新属性添加到阵列对象中。在对象中使用for var attr进行迭代时,您正在迭代对象及其所有属性。因此,您的新方法包含在该循环中
您需要为var i=0使用a;我可能的复制品在正常的for循环中也不起作用,谢谢。是的。可能您没有修复循环中出现的所有for…this.group包含什么?也许项目的.toString方法也会出错。最好使用console.log而不是alert来调试代码。在chrome中,按F12打开开发者工具和控制台。在Firefox中,按control+shift+k。然后,您可以将信息添加到您的日志中,如console.login。对于上面已经发布的iAs,这并不能修复它!我仍然收到相同的警报…在init方法中将this.group分配给gruppo之前,您能告诉我们您是如何分配它的吗?我已经使用.join修复了它,无论如何,this.group是通过从复选框中获取值并将其放入数组中来分配的。也许有什么问题,但使用。加入修复它!删除array.prototype.remove并使用后。加入它修复了它,谢谢!但是,你知道为什么即使使用普通for循环也会发生这种情况吗?我没有说要删除Array.prototype.remove。正常循环不会发生这种情况。可能是代码存在缓存问题,也可能是在this.group[i].toString方法中忽略了for的出现?。请提供一个自包含的示例,如果您仍然受到此问题的困扰,请展示此行为。出于某种原因,此示例修复了此问题,不再解析任何随机函数,但我肯定不再使用数组中的变量作为数组,谢谢!它适用于物体吗?是的,它适用于普通物体。Object.prototype永远不会包含可枚举属性。@Bergi在数组中只设置两个相距较远的元素时,这种情况很少见:arr[0]=1;arr[1000]=2您应该在arr{ifarr.hasOwnPropertykey{…在这里做一些事情