如何向此对象添加元素 [英] How can I add elements to this object
本文介绍了如何向此对象添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这个结构
var data =
{
"people": [
{ "name" : "John", "id" : 1 },
{ "name" : "Marc", "id" : 2 }
]
}
我想在JavaScript中为此添加更多元素,特别是在jQuery中,然后像这样发送它
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);
这篇关于如何向此对象添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文