Javascript 添加新对象时循环和条件?

Javascript 添加新对象时循环和条件?,javascript,Javascript,我尝试这样做: posts[fbposts[fbpost].id] = ({ name: fbposts[fbpost].from.name, link: "http://www.facebook.com/" + fbposts[fbpost].from.id, img: "http://graph.facebook.com/" + fbposts[fbpost].from.id + "/picture", message: fbposts[fbpost].mes

我尝试这样做:

posts[fbposts[fbpost].id] = ({
    name: fbposts[fbpost].from.name,
    link: "http://www.facebook.com/" + fbposts[fbpost].from.id,
    img: "http://graph.facebook.com/" + fbposts[fbpost].from.id + "/picture",
    message: fbposts[fbpost].message,
    to: ({
        name: name,
        link: link,
    }),
    created: timeDifference(Date.parse((fbposts[fbpost].updated_time))),
    sortvar: (Date.parse(fbposts[fbpost].updated_time)),
    comments: ({
        /* looping through the comments*/
        if (fbposts[fbpost].comments.count) {
            for (var comment in fbposts[fbpost].comments.data) {
                name: fbposts[fbpost].comments.data[comment].from.name;
                link: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id;
                img: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id; + "/picture";
                message: fbposts[fbpost].comments.data[comment].message;
                created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time));
            }
        }
    })
});
}

但很明显,当我看到注释时,脚本停止了,因为我不能在对象声明中运行循环和条件。我该怎么做呢

您应该创建一个传递必要参数的函数,并将其用于
注释
属性,例如:

posts[fbposts[fbpost].id] = ({
    name: fbposts[fbpost].from.name,
    link: "http://www.facebook.com/" + fbposts[fbpost].from.id,
    img: "http://graph.facebook.com/" + fbposts[fbpost].from.id + "/picture",
    message: fbposts[fbpost].message,
    to: ({
        name: name,
        link: link,
    }),
    created: timeDifference(Date.parse((fbposts[fbpost].updated_time))),
    sortvar: (Date.parse(fbposts[fbpost].updated_time)),
    comments: ({
        /* looping through the comments*/
        if (fbposts[fbpost].comments.count) {
            for (var comment in fbposts[fbpost].comments.data) {
                name: fbposts[fbpost].comments.data[comment].from.name;
                link: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id;
                img: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id; + "/picture";
                message: fbposts[fbpost].comments.data[comment].message;
                created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time));
            }
        }
    })
});
}
comments: (/* return value from your function */)

事实上,你可以用闭包来做这个

posts[fbposts[fbpost].id] = ({
    name: fbposts[fbpost].from.name,
    link: "http://www.facebook.com/" + fbposts[fbpost].from.id,
    img: "http://graph.facebook.com/" + fbposts[fbpost].from.id + "/picture",
    message: fbposts[fbpost].message,
    to: ({
        name: name,
        link: link,
    }),
    created: timeDifference(Date.parse((fbposts[fbpost].updated_time))),
    sortvar: (Date.parse(fbposts[fbpost].updated_time)),
    comments: ({
        /* looping through the comments*/
        if (fbposts[fbpost].comments.count) {
            for (var comment in fbposts[fbpost].comments.data) {
                name: fbposts[fbpost].comments.data[comment].from.name;
                link: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id;
                img: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id; + "/picture";
                message: fbposts[fbpost].comments.data[comment].message;
                created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time));
            }
        }
    })
});
}
comments: function() { ... looping code ...; return data}(),
这将定义一个内联(一次性使用)函数并立即调用它,结果返回有效的javascript

posts[fbposts[fbpost].id] = ({
    name: fbposts[fbpost].from.name,
    link: "http://www.facebook.com/" + fbposts[fbpost].from.id,
    img: "http://graph.facebook.com/" + fbposts[fbpost].from.id + "/picture",
    message: fbposts[fbpost].message,
    to: ({
        name: name,
        link: link,
    }),
    created: timeDifference(Date.parse((fbposts[fbpost].updated_time))),
    sortvar: (Date.parse(fbposts[fbpost].updated_time)),
    comments: ({
        /* looping through the comments*/
        if (fbposts[fbpost].comments.count) {
            for (var comment in fbposts[fbpost].comments.data) {
                name: fbposts[fbpost].comments.data[comment].from.name;
                link: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id;
                img: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id; + "/picture";
                message: fbposts[fbpost].comments.data[comment].message;
                created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time));
            }
        }
    })
});
}
var x = {
    ten: 10, 
    thirty: function(){var a = 10; a += 20; return a}(), 
    fifty: 25+25};
x现在是一个对象,看起来像:

posts[fbposts[fbpost].id] = ({
    name: fbposts[fbpost].from.name,
    link: "http://www.facebook.com/" + fbposts[fbpost].from.id,
    img: "http://graph.facebook.com/" + fbposts[fbpost].from.id + "/picture",
    message: fbposts[fbpost].message,
    to: ({
        name: name,
        link: link,
    }),
    created: timeDifference(Date.parse((fbposts[fbpost].updated_time))),
    sortvar: (Date.parse(fbposts[fbpost].updated_time)),
    comments: ({
        /* looping through the comments*/
        if (fbposts[fbpost].comments.count) {
            for (var comment in fbposts[fbpost].comments.data) {
                name: fbposts[fbpost].comments.data[comment].from.name;
                link: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id;
                img: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id; + "/picture";
                message: fbposts[fbpost].comments.data[comment].message;
                created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time));
            }
        }
    })
});
}
ten: 10
thirty: 30
fifty: 50