如何向此对象添加元素

时间:2011-11-09 02:19:48

标签: javascript jquery

如果我有这个结构

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

我想在JavaScript中添加更多元素,特别是在jQuery中,然后像这样发送它

var dataString = JSON.stringify(data);
$.post('some.php', { data: dataString}, showResult, "text");

1 个答案:

答案 0 :(得分:3)

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/