从数组javascript中删除项目 [英] remove item from array javascript

查看:53
本文介绍了从数组javascript中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从数组中删除一些项目,

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--;
     }

} 

结果是

   BOM = [0,0,0,1];

预期结果是

   BOM = [0,0,0];

看起来我做错了,请帮助我.

its looks like i am doing something wrong , Please help me.

谢谢.

推荐答案

尝试一下

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中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆