2016-07-07 104 views
0

我有幾件事情我想用foreach取代(這是一類我正在要求),但是他們沒有一個人工作:的forEach與在

我最初對於代碼(此作品正確顯示內容):

for (var i in education.schools) { 
     $('#education').append(HTMLschoolStart); 

     var formattedName = HTMLschoolName.replace('%data%', education.schools[i].name).replace('#', education.schools[i].url); 
     var formattedLocation = HTMLschoolLocation.replace('%data%', education.schools[i].location); 
     var formattedDegree = HTMLschoolDegree.replace('%data%', education.schools[i].degree); 
     var formattedMajors = HTMLschoolMajor.replace('%data%', education.schools[i].majors); 
     var formattedDates = HTMLschoolDates.replace('%data%', education.schools[i].dates); 
     var formattedNameDegree = formattedName + formattedDegree; 


     $('.education-entry:last').append(formattedNameDegree); 
     $('.education-entry:last').append(formattedDates); 
     $('.education-entry:last').append(formattedLocation); 
     $('.education-entry:last').append(formattedMajors); 
    } 

我的forEach變化(不顯示任何內容,所有內容將消失):

education.schools.forEach(function() { 
     $('#education').append(HTMLschoolStart); 

     var formattedName = HTMLschoolName.replace('%data%', education.schools[i].name).replace('#', education.schools[i].url); 
     var formattedLocation = HTMLschoolLocation.replace('%data%', education.schools[i].location); 
     var formattedDegree = HTMLschoolDegree.replace('%data%', education.schools[i].degree); 
     var formattedMajors = HTMLschoolMajor.replace('%data%', education.schools[i].majors); 
     var formattedDates = HTMLschoolDates.replace('%data%', education.schools[i].dates); 
     var formattedNameDegree = formattedName + formattedDegree; 


     $('.education-entry:last').append(formattedNameDegree); 
     $('.education-entry:last').append(formattedDates); 
     $('.education-entry:last').append(formattedLocation); 
     $('.education-entry:last').append(formattedMajors); 
    }) 

第二個是一個if語句,我應該更換的一部分與forEach。

if語句工作正常,像這樣唯一的問題是它只有整整7個項目好,如果我再添加到陣列我也將必須更新此:

if (bio.skills.length > 0) { 
     $('#header').append(HTMLskillsStart); 

     var formattedSkill = HTMLskills.replace('%data%', bio.skills[0]); 
     $('#skills').append(formattedSkill); 
     var formattedSkill = HTMLskills.replace('%data%', bio.skills[1]); 
     $('#skills').append(formattedSkill); 
     var formattedSkill = HTMLskills.replace('%data%', bio.skills[2]); 
     $('#skills').append(formattedSkill); 
     var formattedSkill = HTMLskills.replace('%data%', bio.skills[3]); 
     $('#skills').append(formattedSkill); 
     var formattedSkill = HTMLskills.replace('%data%', bio.skills[4]); 
     $('#skills').append(formattedSkill); 
     var formattedSkill = HTMLskills.replace('%data%', bio.skills[5]); 
     $('#skills').append(formattedSkill); 
     var formattedSkill = HTMLskills.replace('%data%', bio.skills[6]); 
     $('#skills').append(formattedSkill); 
    } 

foreach循環碼(代替陣列中顯示每個項目的它顯示陣列中的所有項目七次,基本上它似乎來遍歷它的正確的次數,但所有7項輸出到每次迭代):

if (bio.skills.length > 0) { 
     $('#header').append(HTMLskillsStart); 

     bio.skills.forEach(function(){ 
      var formattedSkill = HTMLskills.replace('%data%', bio.skills); 
      $('#skills').append(formattedSkill); 
     }) 
    } 
+2

因爲'我'不存在於forEach – Li357

+0

[不要使用'for ... in'列舉數組!](https://stackoverflow.com/q/500504/1048572) – Bergi

回答

1

嘗試聲明中的索引210的回調函數:

education.schools.forEach(function(val, i) { 
    $('#education').append(HTMLschoolStart); 

    var formattedName = HTMLschoolName.replace('%data%', education.schools[i].name).replace('#', education.schools[i].url); 
    var formattedLocation = HTMLschoolLocation.replace('%data%', education.schools[i].location); 
    var formattedDegree = HTMLschoolDegree.replace('%data%', education.schools[i].degree); 
    var formattedMajors = HTMLschoolMajor.replace('%data%', education.schools[i].majors); 
    var formattedDates = HTMLschoolDates.replace('%data%', education.schools[i].dates); 
    var formattedNameDegree = formattedName + formattedDegree; 


    $('.education-entry:last').append(formattedNameDegree); 
    $('.education-entry:last').append(formattedDates); 
    $('.education-entry:last').append(formattedLocation); 
    $('.education-entry:last').append(formattedMajors); 
}); 

Mozilla

回調調用與三個參數:

  • 元素值

  • 元素索引

  • 陣列正被遍歷

EDIT

Andreas評價提到,如果forEach運行關閉education.schools陣列,那麼可以使用所述第一參數(val)在回調中代替education.schools[i]獲取當前項目。

+0

爲什麼不你直接使用當前值而不是'education.schools [i]'? – Andreas

+0

@Andreas雖然這會起作用,但OP的問題似乎源於對如何獲得'forEach'回調索引的不清晰。 –

+1

這不應該被認爲是一個投訴^^但是,爲什麼我們不顯示OP的下一步,使'forEach'構造更容易處理:) – Andreas

0

的每個回調得到三個參數傳遞:

  • CurrentValue的 - 陣列中的當前元素
  • 索引 - 所述ucrrent值
  • 陣列的索引 - 的陣列本身

所以你應該使用currentValue.url而不是education.schools[i].name

education.schools.forEach(function(currentValue) { 
    $('#education').append(HTMLschoolStart); 

    var formattedName = HTMLschoolName.replace('%data%', currentValue.name).replace('#', currentValue.url); 
    var formattedLocation = HTMLschoolLocation.replace('%data%', currentValue.location); 
    var formattedDegree = HTMLschoolDegree.replace('%data%', currentValue.degree); 
    var formattedMajors = HTMLschoolMajor.replace('%data%', currentValue.majors); 
    var formattedDates = HTMLschoolDates.replace('%data%', currentValue.dates); 
    var formattedNameDegree = formattedName + formattedDegree; 


    $('.education-entry:last').append(formattedNameDegree); 
    $('.education-entry:last').append(formattedDates); 
    $('.education-entry:last').append(formattedLocation); 
    $('.education-entry:last').append(formattedMajors); 
}) 
0

您forEach中的問題是您沒有定義i。要解決這個問題,您應該替換現有變量的i,或者定義i

你的代碼可能是這樣的:

education.schools.forEach(function() { 
    $('#education').append(HTMLschoolStart); 

    var formattedName = HTMLschoolName.replace('%data%', this.name).replace('#', this.url); 
    var formattedLocation = HTMLschoolLocation.replace('%data%', this.location); 
    var formattedDegree = HTMLschoolDegree.replace('%data%', this.degree); 
    var formattedMajors = HTMLschoolMajor.replace('%data%', this.majors); 
    var formattedDates = HTMLschoolDates.replace('%data%', this.dates); 
    var formattedNameDegree = formattedName + formattedDegree; 


    $('.education-entry:last').append(formattedNameDegree); 
    $('.education-entry:last').append(formattedDates); 
    $('.education-entry:last').append(formattedLocation); 
    $('.education-entry:last').append(formattedMajors); 
}) 

注意,主要的變化是採用this代替education.schools[i]