Javascript Array Splice without changing the index

前端 未结 5 1552
予麋鹿
予麋鹿 2020-12-17 14:20

I am working on a chat and using an array to hold the users. Here is my problem:

User1 joins and is given Index 0 in the array via push. User2 joins and is given Ind

5条回答
  •  不思量自难忘°
    2020-12-17 14:57

    remove array elements without facing re-indexing problem

        var ind=[1,6]; //index positions of elements to remove
        var arr=['a','b','c','d','e','f','g']; // actual array
        var arr2 = arr.filter(function(item,index){
                if(ind.indexOf(index)== -1){
                return true;
        }});
    

    now arr2 is ==========>> ['a','c','d','e','f']

提交回复
热议问题