Remove multiple indices from array

后端 未结 8 2145
一整个雨季
一整个雨季 2020-12-30 12:31

I have an array and I want to remove a bunch of indices

var arr = [0,1,2,3,4,5,6]
var rmIndices = [1,4,5]

What is the best way to remove in

8条回答
  •  死守一世寂寞
    2020-12-30 13:25

    rmIndices.sort({ $1 < $0 })     
    
    for index in rmIndices
    {
        arr.removeAtIndex(index)
    }
    

    Note that I've sorted the indices in descending order. This is because everytime you remove an element E, the indices of the elements beyond E reduce by one.

提交回复
热议问题