Nested JSON: How to add (push) new items to an object?

后端 未结 4 1424
礼貌的吻别
礼貌的吻别 2020-12-02 15:53

I\'m just starting with Arrays, Objects, and JSON - so hopefully there\'s just something simple I\'m overlooking here. I\'m encountering an error when attempting to

4条回答
  •  无人及你
    2020-12-02 15:59

    If your JSON is without key you can do it like this:

    library[library.length] = {"foregrounds" : foregrounds,"backgrounds" : backgrounds};
    

    So, try this:

    var library = {[{
        "title"       : "Gold Rush",
            "foregrounds" : ["Slide 1","Slide 2","Slide 3"],
            "backgrounds" : ["1.jpg","","2.jpg"]
        }, {
        "title"       : California",
            "foregrounds" : ["Slide 1","Slide 2","Slide 3"],
            "backgrounds" : ["3.jpg","4.jpg","5.jpg"]
        }]
    }
    

    Then:

    library[library.length] = {"title" : "Gold Rush", "foregrounds" : ["Howdy","Slide 2"], "backgrounds" : ["1.jpg",""]};
    

提交回复
热议问题