如何在Javascript中将对象推送到数组中

时间:2015-05-30 06:53:42

标签: javascript

我有包含对象的数组,我想向它添加一个新对象。

//here is my array    
var countries = [
    { start: 20.2, end: 20.7, country_name: "United States" },
    { start: 20.7, end: 21.2, country_name: "Canada" }
]

//new object to push in this array    
{ start: 23.2, end: 23.7, country_name: "Peru" }

2 个答案:

答案 0 :(得分:4)

您是否阅读过文档?我希望你下次再试一次。让我们从SUCH琐碎的问题中解脱出来

countries.push({ start: 23.2, end: 23.7, country_name: "Peru" })

答案 1 :(得分:0)

使用以下代码:

var newObj = { start: 23.2, end: 23.7, country_name: "Peru" }
countries.push(newObj);