Reputation: 6398
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.
Upvotes: 2
Views: 443
Reputation: 11
Try using filter:
var test1 = ['a','b','c','d'];
var test2 = ['b','c'];
test2.forEach(removeItem =>
{
test1 = test1.filter(item => item != removeItem);
})
console.log('Modified array',test1);
Upvotes: 1
Reputation: 19282
Array.prototype.remove= function(){
var what, a= arguments, L= a.length, ax;
while(L && this.length){
what= a[--L];
while((ax= this.indexOf(what))!= -1){
this.splice(ax, 1);
}
}
return this;
}
Call this function
for(var i = 0; i < BOM.length; i++)
{
if(BOM[i] === 1)
BOM.remove(BOM[i]);
}
Upvotes: 0
Reputation: 8476
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);
Upvotes: 4