Declare a JSON array of Objects and add items dynamically

后端 未结 4 649
心在旅途
心在旅途 2021-01-26 04:56

This is stupid but i am missing something and could not reach the conclusion. I am trying to initialize a JSON Array and trying to add JSON Objects to it run time. For an exampl

4条回答
  •  感动是毒
    2021-01-26 05:50

    Its simple init first the object or array you want to play dynamicly e.x:

    var myArr = [], myObj = {};
    

    Now if adding element to them:

    myArr.push(10);
    myObj.new = 10 or myObj['new'] = 10
    

    More advanced :

    myArr.push(myObj); //[10, {'new':10}] --> looks our object
    

提交回复
热议问题