Javascript 2d array
Js two-dimensional array creation: as the name implies, a two-dimensional array is to create one or more arrays on the basis of a one-dimensional array. The code is as follows:
var webGroup=[
["01"."Journey to the West"."Cheng 'en Wu"],
["."."Romance of The Three Kingdoms"."Luo Guanzhong"],
["3"."Water Margin"."Nai am"],
["04"."A Dream of Red Mansions"."Cao Xueqin"]]document.write(webGroup+"<br />")
Copy the code
Two traversal methods for a two-dimensional array
Use “for” to iterate through groups of numbers:
var webGroup=[
["01"."Journey to the West"."Cheng 'en Wu"],
["."."Romance of The Three Kingdoms"."Luo Guanzhong"],
["3"."Water Margin"."Nai am"],
["04"."A Dream of Red Mansions"."Cao Xueqin"]]for( var i=0; i<webGroup.length; i++){
document.write("arr["+i+"]:"+"<br />")
for(var j=0; j<webGroup[i].length; j++){
document.write(webGroup[i][j]+" ")}document.write("<hr />")
document.write("<br />")}Copy the code
Use for… In traversal number group:
var webGroup=[
["01"."Journey to the West"."Cheng 'en Wu"],
["."."Romance of The Three Kingdoms"."Luo Guanzhong"],
["3"."Water Margin"."Nai am"],
["04"."A Dream of Red Mansions"."Cao Xueqin"]]var i=""
for(i in webGroup){
var j="";
document.write("arr["+i+"]:"+"<br />")
for(j in webGroup[i]){
document.write(webGroup[i][j]+" ")}document.write("<hr />")
document.write("<br />")}Copy the code
How do I enter a two-dimensional array directly into a table
The code is as follows:
<style>
td {
text-align: center;
}
</style>
</head>
<body>
<script>
var jus = [
["Sequence"."Name"."Gender"."Age"."Native"],
["01"."马东"."Male"."21"."Hebei"],
["."."Yang"."Male"."18"."Shanghai"],
["3"."Li Shang"."Male"."27"."Tianjin"],
["04"."Enzyme Fijian"."Female"."16"."Beijing"]]document.write("<table border='1' width='600' cellspacing='0' cellpadding='5' align='center'> ")
for (var i = 0; i < jus.length; i++) {
if(i%2= =1) {document.write("<tr style='background:pink'>")}else{document.write("<tr style='background:cyan'>")}
for (var j = 0; j < jus[i].length; j++) {
document.write("<td>")
document.write(jus[i][j])
document.write("</td>")}document.write("<br />")
document.write("</tr>")}document.write("</table>")
</script>
</body>
Copy the code
———————————————— Copyright notice: This article is originally published BY CSDN blogger “Hfeidz” under CC 4.0 BY-SA copyright agreement. Please attach the link of the original source and this statement. The original link: blog.csdn.net/Hfeidz/arti…