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

后端 未结 6 1868
轮回少年
轮回少年 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:06

    I use a library called DevBox.Linq.JS

    It basically adds Linq type functionality to Javascript Arrays. For example, it adds "Remove":

       Array.prototype.Remove = function (condition) {
        if (condition == undefined) {
            console.error('Informe a expressão para executar a tarefa.')
            return;
        }
    
        for (var i = 0 ; i < this.length ; i++) {
            if (condition(this[i]))
                this.splice(i, 1);
        }
    }
    

    This way you can use it with a lambda like this:

    myArray.remove(i=>i=="valueToDelete");
    

    Hope it helps, It already has all the functionality so you don't have to deal with that pesky splice.

提交回复
热议问题