You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
스택을 사용하면 가장 나중에 집어넣은 요소가 먼저 나옵니다. 이런 특징을 줄여서 후입선출(Last-In-First-Out, LIFO)이라고 부릅니다. 반면 큐를 사용하면 먼저 집어넣은 요소가 먼저 나오기 때문에 큐는 선입선출(First-In-First-Out, FIFO) 자료구조라고 부릅니다.
155
155
156
-
<<<<<<< HEAD
157
156
자바스크립트 배열을 사용하면 큐와 스택 둘 다를 만들 수 있습니다. 이 자료구조들은 배열의 처음이나 끝에 요소를 더하거나 빼는 데 사용되죠.
158
157
159
158
이렇게 처음이나 끝에 요소를 더하거나 빼주는 연산을 제공하는 자료구조를 컴퓨터 과학 분야에선 [데큐(deque, Double Ended Queue)](https://en.wikipedia.org/wiki/Double-ended_queue)라고 부릅니다.
160
-
=======
161
-
Arrays in JavaScript can work both as a queue and as a stack. They allow you to add/remove elements, both to/from the beginning or the end.
162
-
163
-
In computer science, the data structure that allows this, is called [deque](https://en.wikipedia.org/wiki/Double-ended_queue).
164
-
>>>>>>> upstream/master
165
159
166
160
**아래는 배열 끝에 무언가를 해주는 메서드입니다.**
167
161
@@ -233,11 +227,7 @@ alert( fruits );
233
227
234
228
숫자형 키를 사용함으로써 배열은 객체 기본 기능 이외에도 순서가 있는 컬렉션을 제어하게 해주는 특별한 메서드를 제공합니다. `length`라는 프로퍼티도 제공하죠. 그렇지만 어쨌든 배열의 본질은 객체입니다.
235
229
236
-
<<<<<<< HEAD
237
-
이렇게 배열은 자바스크립트의 일곱 가지 원시 자료형에 해당하지 않고, 원시 자료형이 아닌 객체형에 속하기 때문에 객체처럼 동작합니다.
238
-
=======
239
-
Remember, there are only eight basic data types in JavaScript (see the [Data types](info:types) chapter for more info). Array is an object and thus behaves like an object.
240
-
>>>>>>> upstream/master
230
+
자바스크립트의 원시 자료형은 여덟 가지뿐입니다(자세한 내용은 [자료형](info:types) 챕터 참고). 배열은 원시 자료형이 아니라 객체에 속하므로 객체처럼 동작합니다.
241
231
242
232
예시를 하나 살펴봅시다. 배열은 객체와 마찬가지로 참조를 통해 복사됩니다.
243
233
@@ -253,11 +243,7 @@ arr.push("배"); // 참조를 이용해 배열을 수정합니다.
253
243
alert( fruits ); // 바나나,배 - 요소가 두 개가 되었습니다.
254
244
```
255
245
256
-
<<<<<<< HEAD
257
-
배열을 배열답게 만들어주는 것은 특수 내부 표현방식입니다. 자바스크립트 엔진은 아래쪽 그림에서처럼 배열의 요소를 인접한 메모리 공간에 차례로 저장해 연산 속도를 높입니다. 이 방법 이외에도 배열 관련 연산을 더 빠르게 해주는 최적화 기법은 다양합니다.
258
-
=======
259
-
...But what makes arrays really special is their internal representation. The engine tries to store its elements in the contiguous memory area, one after another, just as depicted on the illustrations in this chapter, and there are other optimizations as well, to make arrays work really fast.
260
-
>>>>>>> upstream/master
246
+
배열을 배열답게 만들어주는 것은 특수 내부 표현 방식입니다. 자바스크립트 엔진은 아래쪽 그림에서처럼 배열의 요소를 인접한 메모리 공간에 차례로 저장해 연산 속도를 높입니다. 이 방법 이외에도 배열 관련 연산을 더 빠르게 해주는 최적화 기법은 다양합니다.
261
247
262
248
그런데 개발자가 배열을 '순서가 있는 자료의 컬렉션'처럼 다루지 않고 일반 객체처럼 다루면 이런 기법들이 제대로 동작하지 않습니다.
263
249
@@ -295,11 +281,7 @@ fruits.age = 25; // 임의의 이름을 사용해 프로퍼티를 만듭니다.
295
281
fruits.shift(); // 배열 맨 앞의 요소를 빼줍니다.
296
282
```
297
283
298
-
<<<<<<< HEAD
299
284
`shift` 메서드를 호출한 것과 동일한 효과를 보려면 인덱스가 `0`인 요소를 제거하는 것만으론 충분하지 않습니다. 제거 대상이 아닌 나머지 요소들의 인덱스를 수정해 줘야 하죠.
300
-
=======
301
-
It's not enough to take and remove the element with the index `0`. Other elements need to be renumbered as well.
대괄호 `[]`를 사용하면 더 짧은 문법으로 배열을 만들 수 있기 때문에 `new Array()`는 잘 사용되지 않는 편입니다. `new Array()`엔 다루기 까다로운 기능도 있어서 더욱더 그렇습니다.
422
-
=======
423
-
It's rarely used, because square brackets `[]` are shorter. Also, there's a tricky feature with it.
424
-
>>>>>>> upstream/master
402
+
대괄호 `[]`를 사용하면 더 짧은 문법으로 배열을 만들 수 있기 때문에 `new Array`는 잘 사용되지 않는 편입니다. `new Array`엔 다루기 까다로운 기능도 있어서 더욱더 그렇습니다.
425
403
426
404
숫자형 인수 하나를 넣어서 `new Array`를 호출하면 배열이 만들어지는데, 이 배열엔 *요소가 없는 반면 길이는 인수와 같아*집니다.
427
405
428
-
<<<<<<< HEAD
429
-
예시를 통해 `new Array()`의 이런 특징이 어떻게 실수를 유발할 수 있는지 알아봅시다.
430
-
=======
431
-
Let's see how one can shoot themselves in the foot:
432
-
>>>>>>> upstream/master
406
+
예시를 통해 `new Array`의 이런 특징이 어떻게 실수를 유발할 수 있는지 알아봅시다.
433
407
434
408
```js run
435
409
let arr =newArray(2); // 이렇게 하면 배열 [2]가 만들어질까요?
@@ -439,13 +413,7 @@ alert( arr[0] ); // undefined가 출력됩니다. 요소가 하나도 없는 배
439
413
alert( arr.length ); // 길이는 2입니다.
440
414
```
441
415
442
-
<<<<<<< HEAD
443
-
위 예시에서 확인해 본 것처럼 `new Array(number)`를 이용해 만든 배열의 요소는 모두 `undefined` 입니다.
444
-
445
416
이런 뜻밖의 상황을 마주치지 않기 위해 `new Array`의 기능을 잘 알지 않는 한 대부분의 개발자가 대괄호를 써서 배열을 만듭니다.
446
-
=======
447
-
To avoid such surprises, we usually use square brackets, unless we really know what we're doing.
448
-
>>>>>>> upstream/master
449
417
450
418
## 다차원 배열
451
419
@@ -458,11 +426,7 @@ let matrix = [
458
426
[7, 8, 9]
459
427
];
460
428
461
-
<<<<<<<HEAD
462
-
alert( matrix[1][1] ); // 5, 중심에 있는 요소
463
-
=======
464
-
alert( matrix[0][1] ); // 2, the second value of the first inner array
465
-
>>>>>>> upstream/master
429
+
alert( matrix[0][1] ); // 2, 첫 번째 내부 배열의 두 번째 값
466
430
```
467
431
468
432
## toString
@@ -497,100 +461,77 @@ alert( "1" + 1 ); // "11"
497
461
alert( "1,2"+1 ); // "1,21"
498
462
```
499
463
500
-
<<<<<<< HEAD
501
-
## 요약
502
-
=======
503
-
## Don't compare arrays with ==
504
-
505
-
Arrays in JavaScript, unlike some other programming languages, shouldn't be compared with operator `==`.
464
+
## ==로 배열을 비교하지 마세요
506
465
507
-
This operator has no special treatment for arrays, it works with them as with any objects.
466
+
자바스크립트에서 배열은 다른 몇몇 프로그래밍 언어와 달리 `동등 연산자(==)`로 비교하면 안 됩니다.
508
467
509
-
Let's recall the rules:
468
+
`==`는 배열을 특별하게 취급하지 않습니다. 배열도 일반 객체처럼 처리됩니다.
510
469
511
-
- Two objects are equal `==` only if they're references to the same object.
512
-
- If one of the arguments of `==` is an object, and the other one is a primitive, then the object gets converted to primitive, as explained in the chapter <info:object-toprimitive>.
513
-
- ...With an exception of `null` and `undefined` that equal `==` each other and nothing else.
470
+
규칙을 다시 떠올려 봅시다.
471
+
- 두 객체는 동일한 객체를 참조할 때만 `==` 비교 결과가 `true`입니다.
472
+
-`==`의 피연산자 중 하나가 객체이고 다른 하나가 원시값이면, <info:object-toprimitive> 챕터에서 설명한 것처럼 객체가 원시값으로 변환됩니다.
473
+
- 단, `null`과 `undefined`는 예외입니다. 서로에 대해서만 `==` 비교 시 `true`를 반환하고 그 외의 값과 비교했을 때는 `false`를 반환합니다.
514
474
515
-
The strict comparison`===` is even simpler, as it doesn't convert types.
475
+
엄격한 비교 연산자`===`는 더 단순합니다. 타입 변환을 수행하지 않기 때문입니다.
516
476
517
-
So, if we compare arrays with `==`, they are never the same, unless we compare two variables that reference exactly the same array.
477
+
따라서 정확히 같은 배열을 참조하는 두 변수를 비교하는 경우가 아니라면, 배열을 `==`로 비교했을 때 두 배열은 절대 같지 않습니다.
518
478
519
-
For example:
479
+
예시:
520
480
```js run
521
481
alert( [] == [] ); // false
522
482
alert( [0] == [0] ); // false
523
483
```
524
484
525
-
These arrays are technically different objects. So they aren't equal. The `==`operator doesn't do item-by-item comparison.
485
+
위 배열들은 엄밀히 말하면 서로 다른 객체입니다. 따라서 같지 않습니다. `==`연산자는 배열 요소를 하나씩 비교하지 않습니다.
526
486
527
-
Comparison with primitives may give seemingly strange results as well:
487
+
원시값과 비교할 때도 이상해 보이는 결과가 나올 수 있습니다.
528
488
529
489
```js run
530
490
alert( 0== [] ); // true
531
491
532
492
alert('0'== [] ); // false
533
493
```
534
494
535
-
Here, in both cases, we compare a primitive with an array object. So the array `[]` gets converted to primitive for the purpose of comparison and becomes an empty string `''`.
495
+
여기서는 두 경우 모두 원시값과 배열 객체를 비교하고 있습니다. 따라서 비교를 위해 배열 `[]`가 원시값으로 변환되며, 빈 문자열 ``이 됩니다.
536
496
537
-
Then the comparison process goes on with the primitives, as described in the chapter <info:type-conversions>:
497
+
그 후 비교는 <info:type-conversions> 챕터에서 설명한 원시값 비교 규칙에 따라 진행됩니다.
538
498
539
499
```js run
540
-
//after [] was converted to ''
541
-
alert( 0=='' ); // true, as '' becomes converted to number 0
500
+
//[]가 ''로 변환된 이후
501
+
alert( 0=='' ); // true, ''가 숫자 0으로 변환됨
542
502
543
-
alert('0'=='' ); // false, no type conversion, different strings
503
+
alert('0'=='' ); // false, 타입 변환이 일어나지 않으며 서로 다른 문자열임
544
504
```
545
505
546
-
So, how to compare arrays?
506
+
그렇다면 배열은 어떻게 비교해야 할까요?
547
507
548
-
That's simple: don't use the `==`operator. Instead, compare them item-by-item in a loop or using iteration methods explained in the next chapter.
508
+
간단합니다. `==`연산자를 사용하지 마세요. 대신 반복문이나 다음 챕터에서 설명할 순회 메서드를 사용해 요소를 하나씩 비교하면 됩니다.
549
509
550
-
## Summary
551
-
>>>>>>> upstream/master
510
+
## 요약
552
511
553
512
배열은 특수한 형태의 객체로, 순서가 있는 자료를 저장하고 관리하는 용도에 최적화된 자료구조입니다.
554
513
555
-
<<<<<<< HEAD
556
-
- 선언 방법:
557
-
558
-
```js
559
-
// 대괄호 (가장 많이 쓰이는 방법임)
560
-
let arr = [item1, item2...];
561
-
562
-
// new Array (잘 쓰이지 않음)
563
-
let arr =newArray(item1, item2...);
564
-
```
565
-
566
-
`new Array(number)`을 호출하면 길이가 `number`인 배열이 만들어지는데, 이 때 요소는 비어있습니다.
567
-
=======
568
-
The declaration:
514
+
선언 방법:
569
515
570
516
```js
571
-
// square brackets (usual)
517
+
//대괄호 (가장 많이 쓰이는 방법임)
572
518
let arr = [item1, item2...];
573
519
574
-
// new Array (exceptionally rare)
520
+
// new Array (잘 쓰이지 않음)
575
521
let arr =newArray(item1, item2...);
576
522
```
577
523
578
-
The call to `new Array(number)` creates an array with the given length, but without elements.
579
-
>>>>>>> upstream/master
524
+
`new Array(number)`을 호출하면 길이가 `number`인 배열이 만들어지는데, 이때 요소는 비어 있습니다.
580
525
581
526
-`length` 프로퍼티는 배열의 길이를 나타내줍니다. 정확히는 숫자형 인덱스 중 가장 큰 값에 1을 더한 값입니다. 배열 메서드는 `length` 프로퍼티를 자동으로 조정해줍니다.
582
527
-`length` 값을 수동으로 줄이면 배열 끝이 잘립니다.
583
528
584
-
<<<<<<<HEAD
585
-
다음 연산을 사용하면 배열을 데큐처럼 사용할 수 있습니다.
586
-
=======
587
-
Getting the elements:
588
-
589
-
- we can get element by its index, like `arr[0]`
590
-
- also we can use `at(i)` method that allows negative indexes. For negative values of`i`, it steps back from the end of the array. If`i >= 0`, it works same as `arr[i]`.
529
+
요소에 접근하는 방법:
591
530
592
-
We can use an array as a deque with the following operations:
593
-
>>>>>>> upstream/master
531
+
-`arr[0]`처럼 인덱스를 사용해 요소를 가져올 수 있습니다.
532
+
- 음수 인덱스를 지원하는 `at(i)` 메서드도 사용할 수 있습니다. `i`가 음수이면 배열 끝에서부터 거슬러 올라가며 요소를 찾습니다. `i>=0`인 경우에는 `arr[i]`와 동일하게 동작합니다.
533
+
534
+
다음 연산을 사용하면 배열을 데큐처럼 사용할 수 있습니다.
594
535
595
536
-`push(...items)` -- `items`를 배열 끝에 더해줍니다.
596
537
-`pop()` -- 배열 끝 요소를 제거하고, 제거한 요소를 반환합니다.
@@ -602,12 +543,8 @@ We can use an array as a deque with the following operations:
602
543
-`for (let item of arr)` -- 배열 요소에만 사용되는 모던한 문법입니다.
603
544
-`for (let i in arr)` -- 배열엔 절대 사용하지 마세요.
604
545
605
-
<<<<<<<HEAD
606
-
<info:array-methods> 챕터에선 배열에 요소를 더하거나 빼기, 원하는 요소를 추출하기, 배열 정렬하기 등과 관련된 다양한 메서드를 학습할 예정입니다.
607
-
=======
608
-
To compare arrays, don't use the `==` operator (as well as `>`, `<` and others), as they have no special treatment for arrays. They handle them as any objects, and it's not what we usually want.
546
+
배열을 비교할 때는 `==` 연산자(`>`,`<` 등의 비교 연산자도 마찬가지)를 사용하지 마세요. 이러한 연산자들은 배열을 특별하게 처리하지 않고 일반 객체처럼 다루기 때문에, 우리가 기대하는 방식으로 동작하지 않습니다.
609
547
610
-
Instead you can use `for..of`loop to compare arrays item-by-item.
548
+
대신 `for..of`반복문을 사용해 배열 요소를 하나씩 비교할 수 있습니다.
611
549
612
-
We will continuewith arrays and study more methods to add, remove, extract elements and sort arrays in the next chapter <info:array-methods>.
613
-
>>>>>>> upstream/master
550
+
<info:array-methods> 챕터에선 배열에 요소를 더하거나 빼기, 원하는 요소를 추출하기, 배열 정렬하기 등과 관련된 다양한 메서드를 학습할 예정입니다.
0 commit comments