需要你给点提示-新手来了。
我得到了一个包含学生姓名和考试成绩的模范数组。
我需要执行检查学生是否通过考试的函数(假设score >= 80足以通过),在两种情况下,它都会显示正确的信息-“通过”或“没有通过”。
我需要使用.map结构或"for each“循环来完成此操作。
如果你能帮助我,我将非常感激:)
var students = [['David', 80], ['Vinoth', 77], ['Divya', 88], ['Ishitha', 95], ['Thomas', 68]];发布于 2018-06-03 05:08:16
您可以尝试使用Array.map函数:
students.map([name, score] => ({name, score, passed: score >= 80}));发布于 2018-06-03 05:07:30
我不确定你是想要相同的(只是丰富的)数据结构,还是仅仅是输出,但是如果你想要在现有的嵌套数组上构建结果:
students = students.map(s => {
return [s[0], s[1], s[1] >= 80 ? 'passed' : 'not passed'];
});如果您只想输出学生姓名: passed/not passed,则可以改为使用forEach
发布于 2018-06-03 05:10:58
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script>
$(function(){
var students = [['David', 80], ['Vinoth', 77], ['Divya', 88], ['Ishitha', 95], ['Thomas', 68]];
for (var i = 0; i < students.length; ++i) {
var item = students[i];
if(item[1]>= 80){
console.log(item[0] + ": Passed.");
}else{
console.log(item[0] + ": Didn't pass.");
}
}
});
</script>结果:
David: Passed.
Vinoth: Didn't pass.
Divya: Passed.
Ishitha: Passed.
Thomas: Didn't pass.https://stackoverflow.com/questions/50661129
复制相似问题