Created
April 4, 2024 04:28
-
-
Save livemehere/f4631e0cc1c7ff635ef8b6b1501edda2 to your computer and use it in GitHub Desktop.
Be careful when initialize array with object
This file contains hidden or 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
const _grid: T[][] = Array(cols).fill([]); // X (Each index value has same memory address) | |
const _grid: T[][] = Array.from({ length: cols }, () => []); // OK | |
const _grid: T[][] = Array(cols).fill(undefined).map(() => []); // OK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment