从数组内的数组中获取对象

时间:2014-11-02 22:14:12

标签: javascript jquery json foursquare

我想从Foursquare的API获取一系列地点。到目前为止一切都那么好,但我碰到了一堵我无法弄清楚的墙。希望这里有人可能有个好主意。

我目前的列表显示了来自forsquare列表的场地类型,然后它分解了每个场地的名称和lat / lng。我遇到的问题是从foursquare获取类别。该类别存储在另一个数组内,位于场地阵列内。

这是我到目前为止所做的:

function app(info) {
    $('#content').append(info);
}

function getinfo(venueAPI) {
    $.getJSON(venueAPI, function (data) {
            var arr = [];
            var venuearr = [];
                $('#content').empty();
                console.log(data);
                b = '<br/>';
                categoryArr = data.response.list.categories.items;
                categoryArrLength = data.response.list.categories.items.length;
                console.log(categoryArr);
                console.log(categoryArrLength);
                for (var x = 0; x < categoryArrLength; x++) {
                                                      app(categoryArr[x].category.pluralName + b);
                                                   }
                arr = data.response.list.listItems;
                venuearr = arr.items;

                console.log(venuearr);

                for (var i = 0; i < venuearr.length; i++) {
                                                   ltlng =   L.latLng(venuearr[i].venue.location.lat, venuearr[i].venue.location.lng);
                                                   $('#content').append(ltlng + b);
                                                   app(venuearr[i].venue.name + b);

                }
    });
    getinfo();
}

为每个场地返回这样的JSON:

"venue": {
                            "id": "51db2eca498e76d75a5dfea2",
                            "name": "A Gathering Of Stitches",
                            "contact": {},
                            "location": {
                                "address": "54 Cove St",
                                "lat": 43.66797,
                                "lng": -70.256453,
                                "cc": "US",
                                "city": "Portland",
                                "state": "ME",
                                "country": "United States",
                                "formattedAddress": [
                                    "54 Cove St",
                                    "Portland, ME"
                                ]
                            },
                            "categories": [
                                {
                                    "id": "4bf58dd8d48988d1f4941735",
                                    "name": "Design Studio",
                                    "pluralName": "Design Studios",
                                    "shortName": "Design",
                                    "icon": {
                                        "prefix": "https://ss3.4sqi.net/img/categories_v2/shops/design_",
                                        "suffix": ".png"
                                    },
                                    "primary": true
                                }
                            ],

这就是在屏幕上打印出来的内容:

Pubs
Thrift / Vintage Stores
Design Studios
Coffee Shops
LatLng(43.6565, -70.25313)
Bull Feeney's
LatLng(43.65697, -70.25081)
Andy's Old Port Pub
LatLng(43.6571, -70.25713)
Arabica Coffee
LatLng(43.66797, -70.25645)
A Gathering Of Stitches
LatLng(43.65687, -70.25763)
Find

我尝试了几种不同的想法,但到目前为止似乎没有任何效果。

1 个答案:

答案 0 :(得分:1)

venue.categories[0].name;

小提琴: http://jsfiddle.net/18kfrek5/