2016-11-07 84 views
1

我正在嘗試爲單個玩家創建一個對象,該玩家擁有所有可用季節中的所有玩家排名。從多個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_#作爲對象的自己的一部分?可能會有一到多個季節。

+0

如何添加每個season_#作爲它自己的一部分對象動態?可能會有一到多個季節。你能解釋一下你想達到的目標嗎? – void

+0

@void,我發佈的對象(第二個代碼塊)就是我想要實現的。我認爲我的主要問題在於如何創建一個對象並使用變量作爲關鍵名稱?當我現在通過循環嘗試製作這個對象時,我會一直覆蓋賽季,所以我只得到1個賽季而不是3個 – ntgCleaner

回答

2

如果您需要向對象添加屬性,但屬性名稱是可變的,則可以使用[]語法。就你而言,你可以通過幾個簡單的改變來達到你想要的。

首先,只是初始化對象

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

然後,在你的循環,你可以將屬性添加到seasonsObj.seasons這樣的:

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

擊敗我,但可能需要將'season_'+ i'改爲''season_'+ season_number'取決於OP的要求。 –

+0

完美!正是我在找什麼 - 謝謝! – ntgCleaner

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; 
} 
+0

這個答案也完全正確 - 謝謝! – ntgCleaner