Adding elements to object

前端 未结 17 2242
有刺的猬
有刺的猬 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:42

    My proposition is to use different data structure that proposed already in other answers - it allows you to make push on card.elements and allow to expand card properties:

    let card = {
      elements: [
        {"id":10,"quantity":1}
      ],
    
      //other card fields like 'owner' or something...
    }
    
    card.elements.push({"id":22,"quantity":3})
    
    console.log(card);

提交回复
热议问题