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

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

    Every time you remove one, you're making any further cached indices obsolete because the array from that point forward is reindexed.

    As a result, you're removing the wrong items after the first.

    You should iterate the remove array in reverse instead.

    var i = remove.length;
    while (i--) {
        if (remove[i] != undefined)
            options.series[0].data.splice(remove[i],1);
    }   
    

    Additionally, you can improve the performance and get rid of the undefined test if you simply .push() each index into the remove array...

    remove.push(index);
    

提交回复
热议问题