Skip to content

Instantly share code, notes, and snippets.

@butchi
Created November 9, 2016 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save butchi/62d7c0110903cb94f59195b2968ca714 to your computer and use it in GitHub Desktop.
Save butchi/62d7c0110903cb94f59195b2968ca714 to your computer and use it in GitHub Desktop.
2次元配列の初期化(ES2015) ref: http://qiita.com/butchi_y/items/db3078dced4592872a9c
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;
}
}
var tbl = (new Array(3)).fill((new Array(3)).fill(0));
var arr = (new Array(3)).fill(0);
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
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
var tbl = JSON.parse(JSON.stringify((new Array(3)).fill((new Array(3)).fill(0))));
var tbl = (new Array(3)).fill(0);
tbl.forEach((_, i) => {
tbl[i] = (new Array(3)).fill(0);
});
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