如何动态添加类似于JavaScripts数组推送功能的JavaScript对象?

时间:2013-05-19 19:45:00

标签: javascript jquery

如何添加到data.data.trek.places对象。例如,如果用户触发了我想要添加到此对象的事件,然后能够再次迭代它,但我不知道该怎么做。我找不到与数组推送功能类似的东西。我的应用程序位于主干之上,可能有一个下划线功能吗?

我基本上想做:

data.data.trek.places.push({
                            "url" : "/attraction/mauna-kea-summit/19",
                            "img_src" : "http://trekeffect.images.user_images.s3.amazonaws.com/4_36.jpg",
                            "title" : "Mauna Kea Summit",
                            "time_going" : "1368787333",
                            "time_going_hour" : 1,
                            "time_going_minutes" : 55,
                            "duration" : "3600",
                            "id" : 19,
                            "type" : "attraction",
                            "city_id" : 6
                           });

我来自原始的ajax的json如下:

{
  "data":{
  "trek":{
     "places" : [
        {
           "url" : "/attraction/mauna-kea-summit/19",
           "img_src" : "http://trekeffect.images.user_images.s3.amazonaws.com/4_36.jpg",
           "title" : "Mauna Kea Summit",
           "time_going" : "1368687333",
           "time_going_hour" : 1,
           "time_going_minutes" : 55,
           "duration" : "3600",
           "id" : 19,
           "type" : "attraction",
           "city_id" : 6
        },
        {
           "url" : "/attraction/mauna-kea-resort-golf-course/20",
           "img_src" : "http://trekeffect.images.user_images.s3.amazonaws.com/4_41.jpg",
           "title" : "Mauna Kea Resort Golf Course",
           "time_going" : "1368704533",
           "time_going_hour" : 6,
           "time_going_minutes" : 42,
           "duration" : "5400",
           "id" : 20,
           "type" : "attraction",
           "city_id" : 6
        }
     ]
  }

} }

1 个答案:

答案 0 :(得分:3)

data.data.trek.places 一个数组 它与任何其他数组完全相同,只是它碰巧被对象的属性引用。

您可以像使用任何其他阵列一样使用它。