从数组javascript中删除项目(remove item from array javascript)
我试图从数组中删除一些项目,
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); }; var BOM = [0,1,0,1,0,1,1]; var IDLEN = BOM.length; for(var i = 0; i < IDLEN ;++i) { if( BOM[i] == 1) { BOM.remove(i); //IDLEN--; } }结果是
BOM = [0,0,0,1];预期的结果是
BOM = [0,0,0];看起来我做错了什么,请帮帮我。
谢谢。
I was trying to remove some items from an array ,
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); }; var BOM = [0,1,0,1,0,1,1]; var IDLEN = BOM.length; for(var i = 0; i < IDLEN ;++i) { if( BOM[i] == 1) { BOM.remove(i); //IDLEN--; } }RESULT IS
BOM = [0,0,0,1];expected result is
BOM = [0,0,0];its looks like i am doing something wrong , Please help me.
Thanks.
最满意答案
尝试这个
var BOM = [0,1,0,1,0,1,1]; for(var i = 0; i < BOM.length;i++){ if( BOM[i] == 1) { BOM.splice(i,1); i--; } } console.log(BOM);try this
var BOM = [0,1,0,1,0,1,1]; for(var i = 0; i < BOM.length;i++){ if( BOM[i] == 1) { BOM.splice(i,1); i--; } } console.log(BOM);从数组javascript中删除项目(remove item from array javascript)我试图从数组中删除一些项目,
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); }; var BOM = [0,1,0,1,0,1,1]; var IDLEN = BOM.length; for(var i = 0; i < IDLEN ;++i) { if( BOM[i] == 1) { BOM.remove(i); //IDLEN--; } }结果是
BOM = [0,0,0,1];预期的结果是
BOM = [0,0,0];看起来我做错了什么,请帮帮我。
谢谢。
I was trying to remove some items from an array ,
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); }; var BOM = [0,1,0,1,0,1,1]; var IDLEN = BOM.length; for(var i = 0; i < IDLEN ;++i) { if( BOM[i] == 1) { BOM.remove(i); //IDLEN--; } }RESULT IS
BOM = [0,0,0,1];expected result is
BOM = [0,0,0];its looks like i am doing something wrong , Please help me.
Thanks.
最满意答案
尝试这个
var BOM = [0,1,0,1,0,1,1]; for(var i = 0; i < BOM.length;i++){ if( BOM[i] == 1) { BOM.splice(i,1); i--; } } console.log(BOM);try this
var BOM = [0,1,0,1,0,1,1]; for(var i = 0; i < BOM.length;i++){ if( BOM[i] == 1) { BOM.splice(i,1); i--; } } console.log(BOM);
发布评论