Created
November 9, 2016 09:31
-
-
Save butchi/62d7c0110903cb94f59195b2968ca714 to your computer and use it in GitHub Desktop.
2次元配列の初期化(ES2015) ref: http://qiita.com/butchi_y/items/db3078dced4592872a9c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x, y; | |
var tbl = new Array(3); | |
for(y = 0; y < 3; y++) { | |
tbl[y] = new Array(3); | |
for(x = 0; x < 3; x++) { | |
tbl[y][x] = 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tbl = (new Array(3)).fill((new Array(3)).fill(0)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = (new Array(3)).fill(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tbl = (new Array(3)).fill((new Array(3)).fill(0)); | |
tbl[1][1] = 5; | |
console.log(tbl[1][1]); // => 5 | |
console.log(tbl[2][1]); // => 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var row = (new Array(3)).fill(0); | |
var tbl = [row, row, row]; | |
row[1] = 5; | |
console.log(tbl[1][1]); // => 5 | |
console.log(tbl[2][1]); // => 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tbl = JSON.parse(JSON.stringify((new Array(3)).fill((new Array(3)).fill(0)))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tbl = (new Array(3)).fill(0); | |
tbl.forEach((_, i) => { | |
tbl[i] = (new Array(3)).fill(0); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tbl = new Array(3); | |
for(let y = 0; y < 3; y++) { | |
tbl[y] = new Array(3).fill(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment