1

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.

Red
  • 6,230
  • 12
  • 65
  • 112

3 Answers3

4

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);
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
1
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);
sai nathan
  • 11
  • 2
0
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]);
}
Talha
  • 18,898
  • 8
  • 49
  • 66