Javascript - 加载到数组可能的无限循环

时间:2016-06-22 16:50:36

标签: javascript jquery

所以我试图通过一个循环将一些内容加载到我的数组中但是看起来浏览器选项卡崩溃但我无法确定原因。

这是我的加载功能,

var postArray = "";
var postsCounter = 0;
var post = [];
postArray = [];

LoadPosts: function(){
    //Load user posts
    var tempPosts = localStorage.getItem('posts');
    if(tempPosts){
        postArray = JSON.parse(tempPosts);
    }
    postsCounter = postArray.length;

    //Load items posts
    for(var i = 0; i < items.length; i++){
        post = {
            id: postsCounter,
            title: items[i].title,
            points: items[i].points,
            comments: items[i].comments,
            src: items[i].src,
            type: items[i].type,
            user: undefined
        };
        postArray.push(post);
        postsCounter++;
    }
    PostManager.Exec();
},

我的物品,

var items =
[
    {
        "title":"It's Not Like I'm That Ugly",
        "points": 550,
        "comments": 67,
        "src": "/assets/images/someimage.jpg",
        "type": "funny"
    }
    //It goes on
];

1 个答案:

答案 0 :(得分:-1)

我想出了问题,看来我正在声明项目变量不正确。 它应该是这样的,

var items =
[
    {
        myTitle:"It's Not Like I'm That Ugly",
        myPoints: 550,
        myComments: 67,
        mySrc: "/assets/images/someimage.jpg",
        myType: "funny"
    },
    //It goes on
];