Javascript循环数组

时间:2017-03-09 03:16:31

标签: javascript

我试图弄乱javascript并创建了一个多维有点数组:

var students = [ [['David'], [80]], [['Vinoth'], [77]], [['Goren'],[55]] ];  

然后我创建了一个if语句,用于测试学生成绩是否为F到达。

for (var i = 0; i < students.length; i++) {
  for (var j = 1; j < students.length; j++) {
    document.write(students[i][j] + "<br/>");

    if (students[i][j] < 60) {
      document.write(students[i][j] + " is Grade : F");
    } else if (students[i][j] < 70) {
      document.write(students[i][j] + " is Grade : D");
    } else if (students[i][j] < 80) {
      document.write(students[i][j] + " is Grade : C");
    } else if (students[i][j] < 90) {
      document.write(students[i][j] + " is Grade : B");
    } else if (students[i][j] < 100) {
      document.write(students[i][j] + " is  Grade : A");
    }
  }
}

在我追求输出学生的名字和等级时,我甚至最终创建了三个不起作用的for循环。

我想知道如何实现这个输出:

David's grade is 80 and is C Grade.
Vinoth's grade is 77 and is C Grade.
Goren's grade is 55 and is F Grade.

知道我的代码中缺少什么?

7 个答案:

答案 0 :(得分:3)

其他答案建议简化数组结构。如果您真的想要使用额外的嵌套,则需要进行额外的索引。但是你不需要嵌套循环,因为它仍然只是一个线性结构。

for (var i = 0; i < students.length; i++) {
    var student = students[i][0][0];
    var score = students[i][1][0];
    var grade;
    if (score < 60) {
      grade = "F";
    } else if (score < 70) {
      grade = "D";
    } else if (score < 80) {
      grade = "C";
    } else if (score < 90) {
      grade = "B";
    } else if (score < 100) {
      grade = "A";
    }
    document.write(student + "'s score is " + score + ' and is Grade ' + grade + '<br>');
}

此外,如果您希望得分80生成C成绩而不是B,那么您应该使用<=而不是<比较。

作为一般规则,我建议使用对象而不是数组来处理异构数据,因此您的数组应该是这样的:

var students = [ {
    name: 'David',
    score: 80
}, {
    name: 'Vinoth',
    score: 77
}, {
    name: 'Goren',
    score: 55
}];

然后使用students[i].namestudents[i].score,这更容易理解。

我们的想法是,您可以将数组用于所有相同类型的事物的集合,以及用于收集有关事物的相关信息的对象。

答案 1 :(得分:1)

您的数据结构很奇怪,因为它是深层嵌套的。

我会将它转换为更浅的:

var students = [ [ 'David', 80], ['Vinoth', 77], ['Goren', 55] ];

最好定义一个参数函数来确定成绩函:

function getGrade(value) {
  if (value > 90) return 'A';
  else if (value > 80) return 'B';
  else if (value > 70) return 'C';
  else if (value > 60) return 'D';
  else return 'F'
}

我更喜欢forEach循环,但由于您似乎偏好for,我们可以写:

for (let i = 0; i < students.length; i++) {
  console.log(`${students[i][0]}'s grade is ${students[i][1]} and is a ${getGrade(students[i][1])} Grade`);
}

答案 2 :(得分:1)

您的学生阵列的内部元素(如姓名或成绩)本身都包含在括号中,因此成为数组。取下内支架。

此外,如果您希望得分为80,则您的比较运算符$(document).ready(function(){ $('.txt-input').on('keyup', function(){ if($(this).val().length === 0 ){ $('.erase').hide(); }else{ $('.erase').show(); } }); $(".erase").click(function(){ $('.txt-input').val(''); }); }); 应该变为<。&#39; C&#39;按照你的例子。

&#13;
&#13;
<=
&#13;
&#13;
&#13;

答案 3 :(得分:1)

如果你真的那么,真的想要只用最少的修改来修复你的解决方案而不涉及你的数据,那么你就错过了三件事来获得你想要的输出:

    最后
  • document.write("<br>")
  • 内部循环,直到students.length - 1
  • 作为students[i][j - 1]
  • 访问的stundent的名称

这是完整的固定代码,点击&#34;运行代码段&#34;运行它。在底部

&#13;
&#13;
var students = [ [['David'], [80]], [['Vinoth'], [77]], [['Goren'],[55]] ];

for (var i = 0; i < students.length; i++) {
    for (var j = 1; j < students.length - 1; j++) {
        document.write(students[i][j - 1] + "'s grade is ");

        if (students[i][j] < 60) {
            document.write(students[i][j] + " and is Grade : F");
        } else if (students[i][j] < 70) {
            document.write(students[i][j] + " and is Grade : D");
        } else if (students[i][j] < 80) {
            document.write(students[i][j] + " and is Grade : C");
        } else if (students[i][j] < 90) {
            document.write(students[i][j] + " and is Grade : B");
        } else if (students[i][j] < 100) {
            document.write(students[i][j] + " and is  Grade : A");
        }

    }
    document.write("<br>")

}
&#13;
&#13;
&#13;

答案 4 :(得分:1)

您的数组是嵌套的3级。所以你可能想要运行3个循环。但是第二级只有2个元素,第三级只有1个元素。所以你只能运行一个循环。请参阅以下工作代码段。

&#13;
&#13;
var students = [ [['David'], [80]], [['Vinoth'], [77]], [['Goren'],[55]] ];
for (var i = 0; i < students.length; i++) {
    document.write(students[i][0][0] + " ");

    if (students[i][1][0] < 60) {
      document.write(students[i][1][0] + " is Grade : F" + "<br/>");
    } else if (students[i][1][0] < 70) {
      document.write(students[i][1][0] + " is Grade : D"+ "<br/>");
    } else if (students[i][1][0] < 80) {
      document.write(students[i][1][0] + " is Grade : C"+ "<br/>");
    } else if (students[i][1][0] < 90) {
      document.write(students[i][1][0] + " is Grade : B"+ "<br/>");
    } else if (students[i][1][0] < 100) {
      document.write(students[i][1][0] + " is  Grade : A"+ "<br/>");
    }
}
&#13;
&#13;
&#13;

答案 5 :(得分:1)

对于这种特殊情况,对象数组是更好的数据结构。另外,我已将分数分级为等级转换为函数,这使代码更易于阅读和推理。

function getGrade (score) {
  var grades = [
    {
      letter: 'A',
      minScore: 90
    },
    {
      letter: 'B',
      minScore: 80
    },
    {
      letter: 'C',
      minScore: 70
    },
    {
      letter: 'D',
      minScore: 60
    },
    {
      letter: 'F',
      minScore: 0
    }
  ];
  for (var i = 0; i < grades.length; i++) {
    if (grades[i].minScore <= score) return grades[i].letter;
  }
}

var students = [
  {
    name: 'David',
    score: 80
  },
  {
    name: 'Vinoth',
    score: 77
  },
  {
    name: 'Goren',
    score: 55
  }
];

students.forEach(function (student) {
  var str = student.name +
            "'s score is " +
            student.score +
            " and is " +
            getGrade(student.score) +
            " Grade.";
  console.log(str);
});

答案 6 :(得分:1)

我希望这对你有用

var students = [ [['David'], [80]], [['Vinoth'], [77]], [['Goren'],[55]] ];  
    for (var i = 0; i < students.length; i++) {
         document.write("<br/>"+students[i][0] + "'s");

for(var j = 1; j&lt; students.length; j ++){       的console.log(学生[I] [0]);

if (students[i][j] < 60) {
  document.write(students[i][j] + " is Grade : F");
} 
else if (students[i][j] < 70) {
  document.write(students[i][j] + " is Grade : D");
} else if (students[i][j] < 80) {
  document.write(students[i][j] + " is Grade : C");
} else if (students[i][j] < 90) {
  document.write(students[i][j] + " is Grade : B");
} else if (students[i][j] < 100) {
  document.write(students[i][j] + " is  Grade : A");
}

} }