Adding an object to an array of objects with splice

后端 未结 5 1804
北恋
北恋 2021-01-11 16:37

I have an array of objects that looks like this:

event_id=[{\"0\":\"e1\"},{\"0\",\"e2\"},{\"0\",\"e4\"}];

How do I add an element to that a

5条回答
  •  失恋的感觉
    2021-01-11 17:02

    Since I want to add the object in the middle of the array, I ended with this solution:

    var add_object = {"0": "e5"};
    event_id.splice(n, 0, add_object); // n is declared and is the index where to add the object
    

提交回复
热议问题