javascript array.splice() does not remove element in the array?

后端 未结 6 1866
轮回少年
轮回少年 2021-01-14 06:51

I have a remove[] array which has all the index positions of all the element with 0 in the data array (as below).

data array:

Retail,1,Utilities,1,Fo         


        
6条回答
  •  無奈伤痛
    2021-01-14 07:04

    I think you can do it pretty fast this way:

    var dataArr = ["Retail",1,"Utilities",1,"Food & Restaurant",3,"No Data",4,"Construction",0,"Non-profit",1,"Financial Services",12,"Technology",2,"Law",3,"Religion",3,"Retired",2,"Insurance",0,"Real Estate",2,"Audit",3,"Business Organizations",3,"Media & Marketing",0,"Education",3,"Transportation",0,"Manufacturing",0,"Entertainment & Sports",0,"Architecture & Engineering",0,"Cultural Institutions",0,"Government",0,"Banking",0,"Health Care",0,"Business Services",0];
    
    var item = 0; // value of the item to remove
    
    while (dataArr.indexOf(item) > -1) {
     dataArr.splice(dataArr.indexOf(item), 1);
    }
    
    console.log(dataArr);
    

提交回复
热议问题