배열

heehe·2023년 2월 3일
0

JavaScript

목록 보기
2/8
post-thumbnail

배열

여러개의 데이터를 하나의 변수에 담고 싶을 때 사용

  • 배열에 들어가있는 아이템에는 인덱스 번호가 부여된다. 시작점은 0부터 부여가 된다. 인덱스 번호로 배열에 있는 아이템 접근이 가능하다.

배열 함수

pop : 배열 끝에있는 아이템을 제거, 그 아이템값을 리턴

animals.pop()

push(‘아이템’) : 배열 끝에 아이템 추가, 배열의 최종 길이 리턴

animals.push("Dog")
console.log(animals)

animals.push([“Mosquito”,“Mouse”,“Mule"])
console.log(animals)

includes(‘아이템’): 배열에 아이템이 포함되어있으면 true리턴 아니면 false 리턴

console.log(animals.includes("Cat"))

indexOf(‘아이템’): 아이템의 인덱스 번호를 리턴

animals.splice(animals.indexOf("Spider")

slice(시작점,끝점): 시작점~끝점(미포함) 까지 배열을 복사해서 리턴

  • 기존의 배열을 건들이지 않고 새로운 배열을 만듬 (splice와의 차이점)
animals.slice(animals.indexof("Baboon")

splice(시작점,개수): 시작점부터 개수만큼 실제 배열에서 아이템 제거

animals.splice(animals.indexOf("Spider"),3)
console.log(animals)

length : 배열 함수는 아니지만 배열의 크기를 리턴해주는 속성

profile
성장하고픈 ISFJ

0개의 댓글