开发者

JQuery and JSON - Add element

开发者 https://www.devze.com 2023-01-01 20:11 出处:网络
I have a JSON variable defined as: var myCollection = { \"data\": [ { \"name\":\"Joe\", \"id\":\"1\" }, { \"name\":\"Bill\", \"id\":\"2\" },

I have a JSON variable defined as:

var myCollection = {
  "data": [
    { "name":"Joe", "id":"1" },
    { "name":"Bill", "id":"2" },
    { "name":"Dave", "id":"3" }
  ]
};

I have a JavaScript function that is responsible for adding items to the data element in myCollection. However, I'm not sure how to add a name/id pair to the collection via JavaScript. Can someone show me how to add to a开发者_Python百科 JSON collection via JavaScript?

Thank you!


You can simply call the push method on the "data" array:

myCollection.data.push( { "name":"Jim", "id":"4" } );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号