Adding elements to object

前端 未结 17 2168
有刺的猬
有刺的猬 2020-12-02 04:11

I need to populate a json file, now I have something like this:

{\"element\":{\"id\":10,\"quantity\":1}}

And I need to add another \"elemen

17条回答
  •  时光取名叫无心
    2020-12-02 04:22

    With that row

    var element = {};
    

    you define element to be a plain object. The native JavaScript object has no push() method. To add new items to a plain object use this syntax:

    element[ yourKey ] = yourValue;
    

    On the other hand you could define element as an array using

    var element = [];
    

    Then you can add elements using push().

提交回复
热议问题