简体   繁体   中英

How can I add elements to this object

If I had this structure

var data = 
{ 
    "people": [ 
      { "name" : "John", "id" : 1 },
      { "name" : "Marc", "id" : 2 }
    ] 
}

I want to add more elements to this, in JavaScript, specifically in jQuery to then send it like this

var dataString = JSON.stringify(data);
$.post('some.php', { data: dataString}, showResult, "text");
var data = 
{ 
    "people": [ 
      { "name" : "John", "id" : 1 },
      { "name" : "Marc", "id" : 2 }
    ] 
}
data["people"].push({"name": "foo", "id": 3});

console.log(data);

http://jsfiddle.net/RK4bx/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM