从多个foreach循环创建单个对象

时间:2016-11-07 19:53:34

标签: jquery foreach

我正在尝试为单个玩家创建一个对象,让所有玩家在所有可用季节中排名。

我一直在获取信息并正在迭代它。每个循环都有效,但我认为在迭代过程中我会覆盖对象的一部分。我不确定如何在不覆盖部分对象的情况下获取所有信息。

这是我到目前为止所拥有的:

var playerObj;
var username = page.find('.profile_player-name').text();
if(username) {
    var seasons = page.find('.profile_seasons .tab-pane');
    var seasonsObj;
    $.each(seasons, function(i) {
        var statsObj;
        var season = $(seasons[i]);
        var season_number = season.attr('id');
        var season_playlists = season.find('.row > .col-md-3');
        $.each(season_playlists, function(i) {
            var playlist = $(season_playlists[i]);
            var playlist_title = playlist.find('.panel-title').text();
            var playlist_rating = playlist.find('.profile_tier-name').text().match(/\d+/g);
            if(playlist_rating) {
                playlist_rating = playlist_rating[0];   
            }
            var playlist_rank = playlist.find('.profile_tier-name strong').text();
            var playlist_rank_icon = "http://rocketleaguestats.com/" + playlist.find('img').attr('src');
            statsObj = {
                title:playlist_title, 
                rating:playlist_rating, 
                rank: {rank_name:playlist_rank, rank_icon:playlist_rank_icon}
            };
            console.log(statsObj);
        });
    });
} else {
    return false;
}

这应该返回这样的内容:

object = {
    username: "name",
    seasons:
        season_1: 
            rank: 
                rank_name: "Gold",
                rank_icon: http://....jpg,
            rating: "800",
            title: "Ranked Standard"
        season_2: 
            rank: 
                rank_name: "Gold",
                rank_icon: http://....jpg,
            rating: "800",
            title: "Ranked Standard"
        season_3: 
            rank: 
                rank_name: "Gold",
                rank_icon: http://....jpg,
            rating: "800",
            title: "Ranked Standard"
}

但是我不确定如何添加每个不同的季节作为它自己的一个对象,具有不同的变量名称(season_1 vs season_3)。

如何动态添加每个season_#作为对象的一部分?可能有1到多个季节。

2 个答案:

答案 0 :(得分:2)

如果需要向对象添加属性,但属性名称是可变的,则可以使用[]语法。在您的情况下,您可以通过一些简单的更改来实现您想要的目标。

首先,只是初始化你的对象

var seasonsObj = {
    username: username,
    seasons: {}
};

然后,在循环中,您可以向seasonsObj.seasons添加属性,如下所示:

seasonObj.seasons['season_' + i] = statsObj;

答案 1 :(得分:1)

定义变量说data,现在添加一个键值对,如:

var data = {};
var playerObj;
var username = page.find('.profile_player-name').text();
if(username) {
    var seasons = page.find('.profile_seasons .tab-pane');
    var seasonsObj;
    $.each(seasons, function(i) {
        var statsObj;
        var season = $(seasons[i]);
        var season_number = season.attr('id');
        var season_playlists = season.find('.row > .col-md-3');
        $.each(season_playlists, function(i) {
            var playlist = $(season_playlists[i]);
            var playlist_title = playlist.find('.panel-title').text();
            var playlist_rating = playlist.find('.profile_tier-name').text().match(/\d+/g);
            if(playlist_rating) {
                playlist_rating = playlist_rating[0];   
            }
            var playlist_rank = playlist.find('.profile_tier-name strong').text();
            var playlist_rank_icon = "http://rocketleaguestats.com/" + playlist.find('img').attr('src');
            statsObj = {
                title:playlist_title, 
                rating:playlist_rating, 
                rank: {rank_name:playlist_rank, rank_icon:playlist_rank_icon}
            };
            data["season_"+(i+1)] = statsObj; // Add a key value pair here.
            console.log(statsObj);
        });
    });
} else {
    return false;
}