【发布时间】:2016-11-09 08:44:39
【问题描述】:
我想用 forEach 替换几件东西(这是我正在上的一门课的要求),但其中一件都不起作用:
我的原始 For in 代码(这可以正常工作并显示内容):
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);
})
}
【问题讨论】:
-
因为
i在forEach中不存在
标签: javascript jquery arrays for-loop foreach