JavaScript

javascript Arr(어레이)관련 함수 들 (splice shift,pop,unshift,push, indexOf, lastIndexOf, includes)

jin0choi1216 2021. 2. 13. 20:35

javascript array에서 사용 되는 함수들 

let num = ["one", "two", "three"]

 

1. splice(startIndex, delectCount)

let num = ["one", "two", "three"]
num.splice(1,1)

위와 같이 선언하면 "one"이 제거 된다.

 

또 다른 사용법으로 값의 대체도 가능하다.

splice(startIndex, delectCount, item)

num.splice(1,1,"four", "five")

위와 같이 선언하면 "1"번째 인덱스인 "two"가 사라지고 "four", "five"가 들어온다.

 

 

num.splice(1,0, "four", "five")

조금 더 응용하자면 제거하지 않고 값을 넣는것만도 가능하다.

 

 

2. shift()

arr.shift() 배열의 첫번째 제거

 

3. pop()

arr.pop() 배열의 마지막 제거

 

4. unshift(value)

arr.shift() 배열의 첫번째에 value 추가

 

5. push

arr.push(value) 배열이 마지막에 value 추가

 

6. indexOf, lastIndexOf

arr.indexOf() : arr에서 가장 처음에 있는 인덱스 출력

arr.lastIndexOf : arr에서 가장 마지막부터 가장 먼저 있는 인덱스 출력

arr에 값이 없는 경우 -1 출력

 

7. includes

arr.includes : arr에서 값이 있는지 확인 

 

8. reverse

arr.reverse : arr 뒤집기