利用Array.Push
var personInfo = {}; // Create the object and probably save it in
//localStorage or cookie etc or whatever pref you have,
//You can save it in `data` cache(of your HTML DOM element) too.
var peopleObj=[];
personInfo["people"] = peopleObj; //Save the array to the object onto the key/property "Person"
//Push the data as you need.
peopleObj.push({"name":"whatever",
"company":"foo",
"email":"bar@foo.com"
});
peopleObj.push({"name":"whatever2",
"company":"foo",
"email":"bar@foo.com"});
console.log(personInfo);
演示
这是一个示例演示,说明您可能要实现的目标:- 使用 localStorage 存储骨架并在单击按钮时向其添加数据。
var personInfo = {
"people": []
};
window.localStorage.setItem('person',JSON.stringify(personInfo));
var i = 0;
$('input').click(function () {
var obj = JSON.parse(window.localStorage.getItem('person'));
obj["people"].push({
"name": "whatever" + i++,
"company": "foo",
"email": "bar@foo.com"
});
window.localStorage.setItem('person',JSON.stringify(obj));
console.log(obj);
});