[英]Why is my new object undefined?
I am creating a script to pull certain information from a Facebook stream and insert them into an object I create. 我正在创建一个脚本,以从Facebook流中提取某些信息,并将其插入我创建的对象中。 I would like the object to look like this: 我希望对象看起来像这样:
fbObj {
{"name": "blah", "post": "this is a post"}
{"name": "blah_2", "post": "this is another post"}
}
Is this a good way to create that object? 这是创建该对象的好方法吗?
<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;
}
})
}
fbObj的访问方式像数组,而不是对象,因此应对其进行初始化: var fbObj = []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.