2015-12-04 13 views
-1

我通過循環的一些數據,並創建一個新的數組,像這樣前存在:如果校驗值增加導致陣列

var tableData = [], 
countries = {}; 
fullData.forEach(function (d) { 
    d.gdp = +d.gdp; 
    var country = countries[d.country]; 
    if (!country) { 
     tableData.push(country = countries[d.country] = {}); 
    } 
    country[d.year] = d.value + " <span class='note'>" + d.note + "</span>", 
     countries[d.country]['note'] = d.note, 
     countries[d.country]['GDP'] = d.gdp, 
     countries[d.country].Country = d.country; 
}); 

我只想包括d.note如果它是一個值。數據中有很多對象沒有這個字段。我在哪裏放置這個條件?我不斷收到錯誤。感謝讚賞。

+0

現在我讀過幾次和重新格式化所有的代碼,我仍然不能100%確定你的要價。 – Liam

回答

4

替換該行

country[d.year] = d.value + " <span class='note'>" + d.note + "</span>", 
    countries[d.country]['note'] = d.note, 
     countries[d.country]['GDP'] = d.gdp, 
    countries[d.country].Country = d.country; 

if (d.note) 
{ 
    country[d.year] = d.value + " <span class='note'>" + d.note + "</span>"; 
    countries[d.country]['note'] = d.note; 
} 
countries[d.country]['GDP'] = d.gdp; 
countries[d.country].Country = d.country;