2016-03-03 66 views
0

我在幾個地方使用每個函數,因爲我顯示多個結果,但在這種情況下,我知道我只有一個結果,但是我仍然需要將data.results轉換爲index.value,以便稍後用於該功能並將值發送給其他功能。jQuery替代每個?

function displayGameDetails(data){ 
    $returnedData = processObjects(data); 
    var $gameDeveloperName = $returnedData[0]; 
    var $gameDeveloperURL = $returnedData[1]; 
    var $gamePublisher = $returnedData[2]; 
    var $gamePublisherURL = $returnedData[3]; 
    var $gameFranchises = $returnedData[4]; 
    var $gameDeveloperName = $returnedData[5]; 
    var $gameImageURLS = $returnedData[6]; 

    $.each(data.results, function(index, value){ //this is where there will only be 1 result 
     var $boxArt = validateBoxArt(value); 
     var $releaseDate = trimDate(value, "DMY"); 

     $('#game-title, #purchase-amazon').append(value.name); 
     $('#game-synopsis').append(value.deck); 
     $('#game-release-year').append($releaseDate); 
     $('#game-box-art').append('<img src="' + $boxArt + '"/>'); 

     $.each($gameDeveloperName, function(index, developerValue){ 
      $('#game-developers').append('<li><a href="' + developerValue + '">' + $gameDeveloperName + '</a></li>'); 
     }); 

     $.each($gameImageURLS, function(index, imageValue){ 
      $('#image-carousel').append('<div class="item">' + 
             '<img src="' + imageValue + '" alt="test">' + 
             '</div>'); 
     }); 
    }); 
} 

什麼可以替換每個功能,使價值仍將被定義?

回答

0
value = data.results; 

這是我想出的一種解決方法,但效率不高。