-
ARRAYSJava Script 2020. 6. 30. 17:34
1. array literal
- array 는 대괄호 []와 내부 내용으로 표시됩니다.
- array 내부의 각 컨텐트 항목을 요소(element) 라고합니다 .
- 위의 예에서, array 안에는 서로 다른 세 가지 element가 있습니다.
- array 내부의 각 요소는 다른 데이터 유형을 가질 수 있습니다.array 을 변수에 저장할 수도 있습니다.
let newYearsResolutions = ['Keep a journal', 'Take a falconry class', 'Learn to juggle'];
2. Accessing Elements
배열의 각 요소에는 index 로 알려진 번호가 매겨진 위치가 있습니다 .
index를 사용하여 개별 항목에 액세스 할 수 있는데,
이는 항목의 위치를 기준으로 목록에서 항목을 참조하는 것과 유사합니다.
JavaScript의 배열은 index 가 0이므로 위치가 0 에서 계산 되기 시작합니다.
따라서 배열의 첫 번째 항목은 위치에 0있습니다.
-cities는 세 개의 elements가 있는 array 입니다.
- 대괄호 표기법을 []사용하고 있으며 array 이름 뒤에 index를 사용하여 elements에 액세스합니다.
- cities[0]은 cities array 의 index 0 에 있는 element에 액세스합니다= 'New York'.
- 대괄호 표기법과 색인을 사용하여 문자열의 개별 문자에 액세스 할 수도 있습니다.const hello = 'Hello World'; console.log(hello[6]); // Output: W
3. Update Elements
array의 elements에 액세스하면 해당 값을 업데이트 할 수 있습니다.
let seasons = ['Winter', 'Spring', 'Summer', 'Fall']; seasons[3] = 'Autumn'; console.log(seasons); //Output: ['Winter', 'Spring', 'Summer', 'Autumn']
위의 예에서 seasons배열에는 사계절의 이름이 포함되어 있습니다.
그러나 우리는 'Fall' '대신에 Autumn' 이라고 말하기로 결정했습니다 .
seasons[3] = 'Autumn';은 프로그램이 seasons arry 의 index 3 항목을이미 존재 하는 항목(Fall) 대신 'Autumn'으로 변경하도록 지시합니다 .
4. Arrays with let and const
let 변수
- 선언 된 배열(array)의 요소(elements) 변경 가능.
- 선언 된 변수를 재 할당 가능
let condiments = ['Ketchup', 'Mustard', 'Soy Sauce', 'Sriracha']; <!-- case 1--> condiments[0]='Mayo'; console.log(condiments); // Mayo, Mustard, Soy Sauce, Sriracha <!--case 2--> condiments=['Mayo']; console.log(condiments); // Mayo
const 변수
- 선언 된 배열(array)의 요소(elements) 변경 가능.
- 선언 된 변수를 재 할당 불가능
const utensils = ['Fork', 'Knife', 'Chopsticks', 'Spork']; <!-- case 1--> utensils[0]='Spoon'; console.log(utensils); // Spoon, Knife, Chopsticks, Spork <!-- case 2 --> utensils='Spoon'; console.log(utensils); // errors
5. .length property
const newYearsResolutions = ['Keep a journal', 'Take a falconry class']; console.log(newYearsResolutions.length); // Output: 2
점(.) 표기법을 사용 하여 배열의 length속성에 액세스합니다
배열에 몇 개의 요소가 있는지 알고 싶을 때 .length속성에 액세스 할 수 있습니다 .
6. .push() Method
.push()으로 배열 끝에 항목을 추가 할 수 있습니다.
const itemTracker = ['item 0', 'item 1', 'item 2']; itemTracker.push('item 3', 'item 4'); console.log(itemTracker); // Output: ['item 0', 'item 1', 'item 2', 'item 3', 'item 4'];
7. .pop() Method
.pop()는 배열의 마지막 항목을 제거
const newItemTracker = ['item 0', 'item 1', 'item 2']; const removed = newItemTracker.pop(); console.log(newItemTracker); // Output: [ 'item 0', 'item 1' ] console.log(removed); // Output: item 2
8. .shift()
.shift() method to remove the first item from the array groceryList
const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains']; groceryList.shift(); console.log(groceryList); // output: [ 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains' ]
9. .unshift()
.unshift() method to add 'popcorn' to the beginning of your grocery list.
const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains']; groceryList.unshift('popcorn'); console.log(groceryList); //output [ 'popcorn', 'orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains' ]
10. .indexOf()
find the index of a particular element
const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains']; const pastaIndex=groceryList.indexOf('pasta'); console.log(pastaIndex); // output : 4
↓ Mor Array ↓
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
Array
The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects.
developer.mozilla.org
11. Array and function
Arrays mutated inside of a function will keep that change even outside the function.
const flowers = ['peony', 'daffodil', 'marigold']; function addFlower(arr) { arr.push('lily'); } addFlower(flowers); console.log(flowers); // Output: ['peony', 'daffodil', 'marigold', 'lily']
12. Nested Array
배열에 다른 배열이 포함 된 경우 이를 중첩 배열(Nested Array)이라고 합니다
const nestedArr = [[1], [2, 3]]; console.log(nestedArr[1]); // Output: [2, 3]
estedArr[1]배열 인 인덱스 1의 요소를 잡을 것이다 [2, 3].
그런 다음 중첩 배열 내의 요소에 액세스하려면
인덱스 값을 사용하여 대괄호 표기법을 체인화 하거나 추가 할 수 있습니다 .
const nestedArr = [[1], [2, 3]]; console.log(nestedArr[1]); // Output: [2, 3] console.log(nestedArr[1][0]); // Output: 2
In the second console.log() statement, we have two bracket notations chained to nestedArr.
We know that nestedArr[1] is the array [2, 3].
Then to grab the first element from that array,
we use nestedArr[1][0] and we get the value of 2
'Java Script' 카테고리의 다른 글
[코드카데미] Loops : Whale Talk (0) 2020.07.02 Loops (0) 2020.07.01 SCOPE (0) 2020.06.29 [CODECADEMY] 코드카데미 CODE CHALLENGES: JAVASCRIPT FUNDAMENTALS 2 (0) 2020.06.17 [CODECADEMY] 코드카데미 CODE CHALLENGES: JAVASCRIPT FUNDAMENTALS (0) 2020.06.16