2013-04-02 8 views
0

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; 
     } 
     }) 
     } 
+2

을 당신은'fbObj를 초기화해야한다 [i]는'당신이 그것을 사용하기 전에 즉'fbObj [I] = {}' – Musa

+0

감사합니다. 이것은 일했다 :) – jumbopap

답변

0

fbObj 배열처럼 액세스 할 수 있습니다, 따라서 초기화해야 개체를 좋아하지 : var fbObj = []

관련 문제