How to remove JavaScript array element and reset keys

后端 未结 1 1903
抹茶落季
抹茶落季 2021-02-13 12:59

I have an array like below:

 var fields = [
    {name:\"mark\", age:\"23\"}, 
    {name:\"smith\", age:\"28\"}, 
    {name:\"kelvin\", age:\"25\"}, 
    {name:\"         


        
相关标签:
1条回答
  • 2021-02-13 13:18

    If I am understanding this correctly you want to remove array element at index 2 and re-index the array so there is no empty space. If that's the case javascript has got you covered.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

    fields.splice(2,1); // This modifies your original array and removes only one element starting at index 2. 
    
    0 讨论(0)
提交回复
热议问题