//一张九九乘法表放入json中解析
解析为 // 练习1: 把一张九九乘法表(需要用程序生成)放入JSON中,然后解析 // var obj = { // line1: [‘11=1’], // line2: ['12=2’, ‘22=4’], // line3: ['13=3’, ‘23=6’, '33=9’], // … // }
var o = {
line1:[],
line2:[],
line3:[],
line4:[],
line5:[],
line6:[],
line7:[],
line8:[],
line9:[]
}
for(var i = 1; i <= 9; i++){
for(var j = 1; j <=i;j ++){
switch(i){
case i=1:
o.line1.push(i + '*' + j + '='+i*j);
console.log('循环里打印',o);
break;
case i =2:
o.line2.push(i + '*' + j + '='+i*j);
break;
case i =3:
o.line3.push(i + '*' + j + '='+i*j);
break;
case i =4:
o.line4.push(i + '*' + j + '='+i*j);
break;
case i =5:
o.line5.push(i + '*' + j + '='+i*j);
break;
case i =6:
o.line6.push(i + '*' + j + '='+i*j);
break;
case i =7:
o.line7.push(i + '*' + j + '='+i*j);
break;
case i =8:
o.line8.push(i + '*' + j + '='+i*j);
break;
case i =9:
o.line9.push(i + '*' + j + '='+i*j);
break;
default:
}
}
}
o.line1.push('a');
console.log(o);
var ob = JSON.stringify(o);
document.write(ob);
console.log(ob);
|