Javascript Array Splice without changing the index

前端 未结 5 1557
予麋鹿
予麋鹿 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 15:20

    Use delete instead of splice.

    > a = ['1', '2', '3']
    < Array [ "1", "2", "3" ]
    
    > delete a[1]
    < true
    
    > a
    < Array [ "1", undefined × 1, "3" ]
    
    > a.length
    < 3
    

提交回复
热议问题