为什么我的新对象未定义?

时间:2013-04-02 19:05:30

标签: javascript facebook-javascript-sdk

我正在创建一个脚本来从Facebook流中提取某些信息并将它们插入到我创建的对象中。我希望对象看起来像这样:

fbObj {
        {"name": "blah", "post": "this is a post"}
        {"name": "blah_2", "post": "this is another post"}
       }

这是创建该对象的好方法吗?

 <script language="javascript" type="text/javascript">
        function buildObject() {

           //create the object
            var fbObj = {}
            FB.api('me?fields=feed.fields(story,message,from,updated_time)', function(response) {
                console.log(response)

            //loop through feed, add items to object
        for (i = 0, l = response.feed.data.length; i < l; i++){
            var post = response.feed.data[i];
            if (post.story){
                fbObj[i].name = post.from.name
                fbObj[i].post = post.story
                console.log(fbObj[i])
                }
            if (post.message){
                fbObj[i].name = post.from.name
                fbObj[i].post =  post.message
                console.log(fbObj[i])
        }

        var x = document.getElementById("toObject");
            x.innerHTML=fbObj;
        }
        })
        }

1 个答案:

答案 0 :(得分:0)

fbObj像数组一样被访问,而不是像对象一样,因此应该初始化它:var fbObj = []