-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,440 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
+++ | ||
title = '배열 초기화' | ||
date = 2024-02-28T16:54:26+09:00 | ||
draft = false | ||
author = 'Sehyun' | ||
comments = true | ||
readingTime = true | ||
+++ | ||
|
||
# 배열 초기화 (이중배열) | ||
|
||
배열을 초기화할 때 쓸 수 있는 두 가지 방법: | ||
|
||
- `Array.prototype.fill()` | ||
- `Array.from()` | ||
|
||
## `Array.prototype.fill()` | ||
|
||
배열을 채울 값으로 객체를 받을 경우 그 **참조만 복사**해서 배열을 채운다. | ||
|
||
``` | ||
function solution(n, arr1, arr2) { | ||
const map = new Array(n).fill(new Array(n).fill("*")); | ||
map[1][2] = "H"; | ||
return map; | ||
} | ||
solution(5) | ||
``` | ||
|
||
위 코드의 결과가 | ||
|
||
``` | ||
['*', '*', '*', '*', '*'], | ||
['*', '*', 'H', '*', '*'], | ||
['*', '*', '*', '*', '*'], | ||
['*', '*', '*', '*', '*'], | ||
['*', '*', '*', '*', '*'] | ||
``` | ||
|
||
이게 아니라 | ||
|
||
``` | ||
['*', '*', 'H', '*', '*'], | ||
['*', '*', 'H', '*', '*'], | ||
['*', '*', 'H', '*', '*'], | ||
['*', '*', 'H', '*', '*'], | ||
['*', '*', 'H', '*', '*'] | ||
``` | ||
|
||
이렇게 나온다는 뜻이다! (이렇게 배열을 초기화하면 배열 속의 모든 배열이 똑같은 참조값을 가지게 되어버리기 때문) | ||
|
||
그래서 의도대로 `map[1][2] = "H`를 했을 때 `map[1][2]` 자리의 아이템만 `H`로 바꾸고 싶다면 `Array.prototype.fill()`이 아닌 `Array.from()`을 사용해 배열을 초기화해야 한다. | ||
|
||
``` | ||
function solution(n, arr1, arr2) { | ||
const map = Array.from({ length: n }, () => Array(n).fill("*")); | ||
map[1][2] = "H"; | ||
return map; | ||
} | ||
``` |
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
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.52 KB
...cover_hue930238c74df5cb99104f8eb704cf588_87584_120x120_fill_q75_box_smart1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+272 KB
...about/cover_hue930238c74df5cb99104f8eb704cf588_87584_1600x0_resize_q75_box.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+91.5 KB
.../about/cover_hue930238c74df5cb99104f8eb704cf588_87584_800x0_resize_q75_box.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.