fill Builds Matrices Without Shared Rows

Array.from with a fill(0) inner call gives each matrix row an independent array, while copyWithin moves values inside one array in place with typed bounds.

TypescriptData Integrity

Code

typescriptsnippet
9 lines
// 每行独立:Array.from 工厂 + fill(0) 生成行内元素
const grid: number[][] = Array.from({ length: 3 }, () => new Array<number>(4).fill(0));
grid[1][2] = 7;
console.log(JSON.stringify(grid), '| rows independent:', grid[0] !== grid[1]);

// copyWithin 在同一个数组内部搬移:[1,4,5]←[3,4,5] 从下标 3 拷 2 个到下标 1
const buf: number[] = [1, 2, 3, 4, 5];
const moved = buf.copyWithin(1, 3);
console.log(JSON.stringify(moved), '| in-place:', moved === buf);

Output

Captured from a sandboxed run of this snippet in the build pipeline.

[[0,0,0,0],[0,0,7,0],[0,0,0,0]] | rows independent: true
[1,4,5,4,5] | in-place: true

The code read in motion

The file’s state is a handful of top-level bindings: moved.

What to watch for

  • Imports, when present at all, name standard-library modules only.

Statements that matter

const grid: number[][] = Array.from({ length: 3 }, () => new Array<number>(4).fill(0));

Interactive Tools for This Topic

FAQ

What has to be installed first?
Nothing — the runtime itself provides everything the code touches. Its state lives in module-level bindings: `moved`.