为什么我不能在json-array-object上做foreach?

时间:2014-02-03 15:02:11

标签: javascript

我有一个javascript对象可以像这样输出

console.log(data.feed.entry);

chrome output:

[Object, Object, Object, Object]

我认为[]表示它是一个数组

我尝试像这样循环遍历:

var entries = data.feed.entry;
entries.each(function(entry){
   //console.log(entry);
});

并得到错误" TypeError:对象[object Array]没有方法'每个'"

这是我的目标:

[
    {
        "id": {
            "$t": "https://spreadsheets.google.com/feeds/cells/0Aq_23rNPzvODdFlBOFRYWlQwUFBtcXlGamhQeU9Canc/od6/public/values/R1C1"
        },
        "updated": {
            "$t": "2014-02-03T14:47:31.115Z"
        },
        "category": [
            {
                "scheme": "http://schemas.google.com/spreadsheets/2006",
                "term": "http://schemas.google.com/spreadsheets/2006#cell"
            }
        ],
        "title": {
            "type": "text",
            "$t": "A1"
        },
        "content": {
            "type": "text",
            "$t": "First"
        },
        "link": [
            {
                "rel": "self",
                "type": "application/atom+xml",
                "href": "http://spreadsheets.google.com/feeds/cells/0Aq_23rNPzvODdFlBOFRYWlQwUFBtcXlGamhQeU9Canc/od6/public/values/R1C1"
            }
        ],
        "gs$cell": {
            "row": "1",
            "col": "1",
            "$t": "First"
        }
    },
    {
        "id": {
            "$t": "https://spreadsheets.google.com/feeds/cells/0Aq_23rNPzvODdFlBOFRYWlQwUFBtcXlGamhQeU9Canc/od6/public/values/R1C2"
        },
        "updated": {
            "$t": "2014-02-03T14:47:31.115Z"
        },
        "category": [
            {
                "scheme": "http://schemas.google.com/spreadsheets/2006",
                "term": "http://schemas.google.com/spreadsheets/2006#cell"
            }
        ],
        "title": {
            "type": "text",
            "$t": "B1"
        },
        "content": {
            "type": "text",
            "$t": "asdf"
        },
        "link": [
            {
                "rel": "self",
                "type": "application/atom+xml",
                "href": "http://spreadsheets.google.com/feeds/cells/0Aq_23rNPzvODdFlBOFRYWlQwUFBtcXlGamhQeU9Canc/od6/public/values/R1C2"
            }
        ],
        "gs$cell": {
            "row": "1",
            "col": "2",
            "$t": "asdf"
        }
    },
    {
        "id": {
            "$t": "https://spreadsheets.google.com/feeds/cells/0Aq_23rNPzvODdFlBOFRYWlQwUFBtcXlGamhQeU9Canc/od6/public/values/R2C1"
        },
        "updated": {
            "$t": "2014-02-03T14:47:31.115Z"
        },
        "category": [
            {
                "scheme": "http://schemas.google.com/spreadsheets/2006",
                "term": "http://schemas.google.com/spreadsheets/2006#cell"
            }
        ],
        "title": {
            "type": "text",
            "$t": "A2"
        },
        "content": {
            "type": "text",
            "$t": "second"
        },
        "link": [
            {
                "rel": "self",
                "type": "application/atom+xml",
                "href": "http://spreadsheets.google.com/feeds/cells/0Aq_23rNPzvODdFlBOFRYWlQwUFBtcXlGamhQeU9Canc/od6/public/values/R2C1"
            }
        ],
        "gs$cell": {
            "row": "2",
            "col": "1",
            "$t": "second"
        }
    },
    {
        "id": {
            "$t": "https://spreadsheets.google.com/feeds/cells/0Aq_23rNPzvODdFlBOFRYWlQwUFBtcXlGamhQeU9Canc/od6/public/values/R2C2"
        },
        "updated": {
            "$t": "2014-02-03T14:47:31.115Z"
        },
        "category": [
            {
                "scheme": "http://schemas.google.com/spreadsheets/2006",
                "term": "http://schemas.google.com/spreadsheets/2006#cell"
            }
        ],
        "title": {
            "type": "text",
            "$t": "B2"
        },
        "content": {
            "type": "text",
            "$t": "tada"
        },
        "link": [
            {
                "rel": "self",
                "type": "application/atom+xml",
                "href": "http://spreadsheets.google.com/feeds/cells/0Aq_23rNPzvODdFlBOFRYWlQwUFBtcXlGamhQeU9Canc/od6/public/values/R2C2"
            }
        ],
        "gs$cell": {
            "row": "2",
            "col": "2",
            "$t": "tada"
        }
    }
]

2 个答案:

答案 0 :(得分:5)

您应该使用forEach (MDN)

var entries = data.feed.entry;
entries.forEach(function(entry){
   console.log(entry);
});

或常规for循环:

for (var i = 0, n = entries.length; i < n; i++) {
  console.log(entries[i]);
}

答案 1 :(得分:0)