【JS】【jQuery】 配列 / JSON をループする

jQuery使用しない場合

サンプル

<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript">
var values = [
   ["x", "Math", "English", "Science"],
   ["y", "67", "89", "92"],
];

for (var i in values) {
   for (var j = 0; j < values[i].length; j++) {
      document.write(values[i][j] + '<br>');
   }
}
</script>
</body>
</html>

出力結果

x
Math
English
Science
y
67
89
92

参考文献

http://kreisel.fam.cx/webmaster/clog/2010-06-26-1.html

jQuery使用する場合

サンプル

<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script src="">http://code.jquery.com/jquery.js">
<script type="text/javascript">
var values = [
   ["x", "Math", "English", "Science"],
   ["y", "67", "89", "92"],
];

$.each(values, function(i, elements) {
   $.each(elements, function(j, value) {
      document.write(value + '<br>');
   });
});
</script>
</body>
</html>

出力結果

「サンプル(jQuery使用しない場合)」と同じなので省略。

参考文献

http://www.buildinsider.net/web/jqueryref/011