###実現したいこと
文字列からindexOfで文字探し抜き出し.オブジェクトに格納する.
その際,格納するオブジェクトを5つ以上用意したい.
###現時点でできていること
格納するオブジェクトを3つ用意.
JavaScript
1function Info (x, y,z) { 2 this.x = +x; //オブジェクト 3 this.y = +y; //オブジェクト 4 this.z = +z; //オブジェクト 5} 6 7var str = "9920123A" , infoList = [] , lastIndex = -1; 8while(true){ 9 var x,y; 10 var nextIndex = str.indexOf("20" , lastIndex+1); //20を見つけたら 11 if(nextIndex == -1){ 12 break; //whileループから抜ける 13 }else{ 14 x = str.substr(nextIndex-2 , 2); 15 infoList.push(new Info(x));//格納 16 console.log("x=" + x); 17 18 y = str.substr(nextIndex+2 , 1); 19 infoList.push(new Info(y));//格納 20 console.log("y=" + y); 21 22 z = str.substr(nextIndex+3 , 1); 23 infoList.push(new Info(z));//格納 24 console.log("z=" + z); 25 lastIndex = nextIndex; 26 } 27}
###問題点
"this"で,.x/.y/.zしか使えない.(3つ以上作ることができない)
最終的に
this.? = 99
this.? = 1
this.? = 2
this.? = 3
this.? = A
といったように格納されるようにしたいです.
※読み込む文字列は必ず半角英数です.
以上,修正箇所等ありましたら連絡の程よろしくお願いいたします.