扩展json对象的javascript

时间:2013-05-16 18:24:10

标签: javascript

javascript代码:

window.mybooks={"books": [{"name":"book one","author":"Samuel Peter"},{"name":"book two","author":"Samuel Anderson"}]};

如何向window.mybooks添加新书?

{"name":"book three","author":"Samuel Abelson"}

更新

奇怪,很多相同的答案,而不是为我工作......是什么导致这个?

我确实发现该对象是生成的,可能是错误的。它看起来像这样:

window.mybooks={"books": [{"name":"book one","author":"Samuel Peter"},{"name":"book two","author":"Samuel Anderson"},]};

任何提示如何纠正它和mayabe然后推动将起作用?

4 个答案:

答案 0 :(得分:9)

window.mybooks.books.push({"name":"book three","author":"Samuel Abelson"});

答案 1 :(得分:7)

使用Array.push

var newBook = {"name":"book three","author":"Samuel Abelson"};
mybooks.books.push(newBook );

如果您不在窗口上下文中,请明确使用window.mybooks.books.push(newBook);

答案 2 :(得分:1)

var newBook = {
    "name": "book three",
    "author": "Samuel Abelson"
};
mybooks.books.push(newBook);

答案 3 :(得分:1)

var newBook = {"name":"book three","author":"Samuel Abelson"};
mybooks.books.push(newBook);

试试这个应该有效。使用Javascript Array.push