javascript_배열

NARA·2022년 7월 27일
0

Westudy

목록 보기
8/12

배열선언하는 방법

let myArray = [19, 44, 'good', false]

배열(Array) : 특정한 요소(element)들을 모아놓은 집합

요소(element) : 여러가지 데이터타입 가능

ex) 19, 44, ‘good’, false


순서(index) : 배열안의 요소들의 순서를 0부터 차례로 부여.

ex) 44는 1번 index 요소


요소 접근하기

myArray[1] // 44
접근하고자하는 배열의 이름[index 번호]

요소 수정하기

요소에 접근후 수정하고자하는 값을 할당

myArray[0] = 500

console.log(myArray) // [500, 44, 'good', false]

배열의 길이 구하기

console.log(myArray.length) // 4

중첩된 배열

let myArray = [19, 44, 'good', [100, 200, 300], false]
 
 console.log(myArray[3]) //[100, 200, 300]
 console.log(myArray[3][0]) //100

배열의 마지막요소 접근

console.log(myArray[myArray.length-1]) // 배열의 길이 - 1 은 마지막 인덱스 번호



반복문

: 같은 동작을 반복해서 수행해야할 때 사용

동일한 동작을 조건을 만족할때까지 반복해서 수행

for(let i=0; i<10; i++){
	console.log('Hello wecode!')
} // 'Hello wecode!' 10번 출력

for (초기 상태; 조건; counter 변화) {
	수행할 동작
}

콘솔에 1부터 10까지 1씩 더해가며 출력하기

for(let i=1; i<11; i++){
  console.log(i)

for(let i=0; i<10; i++){
  console.log(i+1)
}

빈 배열 myArray에 100부터 110까지 요소를 추가하기

let myArray = [];

for(let i=100; i<111; i++){
  myArray.push(i)
}

console.log(myArray)

//[100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]


Array의 요소 순회

Array의 요소를 순회하며 콘솔에 출력하기

let colors = ['red', 'blue', 'black', 'white']

for(let i=0; i<colors.length; i++){
  console.log(colors[i])
}

// 순서대로 출력
"red"
"blue"
"black"
"white"

Number로 이루어진 Array의 요소를 순회하며 1씩 더하여 콘솔에 출력하기

let num = [10, 20, 30, 40, 50]

for(let i=0; i<num.length; i++){
  console.log(num[i]+1)
}

//순서대로 출력
11
21
31
41
51

Array의 길이만큼 순회하기

-> 배열의 길이를 이용하여 조건설정

for(i=0; i < 배열의이름.length; i++){

}


배열의 메서드 5가지와 사용 방법

1. slice(start, end)

: 시작점인 요소부터 끝점요소를 제외한 요소를 담은 배열을 리턴

start : 배열의 index의 시작점

end : 배열의 index의 끝점

let nums = [1,2,3,4,5]
let nums_new = nums.slice(1,4)

console.log(nums) // [ 1, 2, 3, 4, 5 ]
console.log(nums_new) // [ 2, 3, 4 ]

// start에 음수가 들어가는 경우

let nums = [1,2,3,4,5]
let nums_new = nums.slice(-2)

console.log(nums) // [ 1, 2, 3, 4, 5 ]
console.log(nums_new) // [ 4, 5 ]

//음수가 들어갈 경우 끝에서부터 해당하는 숫자만큼의 요소를 배열에 담아 리턴

2. splice(start, delete, item)

: 배열 내 특정한 요소 삭제, 다른 요소 대치, 새로운 요소 추가

start : 배열의 index의 시작점

delete : 삭제할 요소의 개수

item : 추가하고 싶은 요소

❗splice 메서드는 필요에 따라 인자를 최소 1개만 쓸 수 있음

let num = [1,2,3,4,5];
num.splice(2,1,10);

console.log(num); // [ 1, 2, 10, 4, 5 ]

3. concat

: 주어진 배열에 기존 배열을 합쳐서 새로운 배열을 반환

const alpha = ['a', 'b', 'c'];

// 배열 2개 이어붙이기 
const arr = [1, [2, 3]];     
alpha.concat(arr);             // [ 'a', 'b', 'c', 1, [ 2, 3 ] ]

// 배열 3개 이어붙이기 
alpha.concat(arr);
alpha.concat(1, [2, 3]);       // [ 'a', 'b', 'c', 1, 2, 3 ]

4. pop

: 배열의 마지막 요소 제거하며 제거된 요소를 반환

let threeArr = [1, 4, 6];
let oneDown = threeArr.pop();

console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]

5. shift

: 배열의 첫번째 요소를 제거

let myArray = [19, 44, 'good', false]

myArray.shift()
console.log(myArray) // [44, 'good', false]

6. push
: 배열에 요소 추가

myArray.push('kiwi')
console.log(myArray) // [19, 44, 'good', false, 'kiwi']

0개의 댓글