Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Last active November 13, 2019 10:13
Show Gist options
  • Save hjzheng/94c2a6ad25fc5a002a6d328a7f7ad90c to your computer and use it in GitHub Desktop.
Save hjzheng/94c2a6ad25fc5a002a6d328a7f7ad90c to your computer and use it in GitHub Desktop.
生成二维数组
function genArray(rows, cols, something) {
// BAD! Rows are copied by reference
// return new Array(rows).fill(new Array(cols).fill(something))
// good way
Array.from(Array(rows), () => new Array(cols).fill(something))
// more inefficient 12%
// return new Array(rows).fill(null).map(() => Array(cols).fill(something))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment