Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extend the json object javascript

Tags:

javascript

javascript code:

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

How to add new book to window.mybooks ?

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

UPDATE:

strange , a lot of same answers, and not working for me... what can cause this ?

I did found that the object is generated, maybe incorrectly. It looks like this:

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

any hints how to correct it and mayabe then push will work ?

like image 792
Jigberto Avatar asked Nov 23 '25 06:11

Jigberto


1 Answers

window.mybooks.books.push({"name":"book three","author":"Samuel Abelson"});
like image 131
Sébastien Renauld Avatar answered Nov 25 '25 18:11

Sébastien Renauld



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!