본문 바로가기
자바스크립트

배열함수 개념과 APIs

by 찬찬2 2020. 9. 9.

www.youtube.com/watch?v=yOdAVDuHUKQ

[looping]

1. for( ){ }

2. for( let 임의의 변수명 of 정의된 변수명(배열) ){ }

3. forEach( ①콜백함수(3가지의 인자: value, index, array), ②Argument ){ }

 

[Addition, deletion, copy]

1. add an item to the end : push( )

2. remove an item from the end : pop( )

3. add an item to the beginning : unshift( )

4. remove an item from the beginning : shift( )

** push와 pop은 마지막 데이터를 사용하기 때문에 비교적 빠르다. 하지만 unshift와 shift는 앞에서 부터 빼고 더하기 때문에 느리다.

 

5. remove an item by index position : splice(start: number, deleteCount: number(몇개를 지울지), addData(지워진 자리에 데이터를 추가) )

6. combine two arrays : concat( 변수명(배열) )

   ex: const fruit = [ 1, 2 ] const fruit2 = [ 3, 4 ] const newfruit = fruit.concat( fruit2 )

 

[search]

fruit.indexOf( 찾고자 하는 데이터 ) : 찾고자 하는 데이터가 있으면 해당 데이터의 index값을 숫자로 반환 없으면 -1을 반환함.

fruit.lastIndexOf( ) 찾고자 하는 데이터가 여러개인 경우 제일 뒤에 있는 데이터의 

fruit.includes( 찾고자 하는 데이터 ) : 찾고자 하는 데이터를 찾으면 true, 없으면 false를 반환함.

 

www.youtube.com/watch?v=3CUjtKJ7PJg&t=151s

8. make a string out of an array : join( '구분자(seperator)' )

const fruits = [ 'apple', 'banna', 'orange' ]

const result = fruits.join( )

9. make an array out of a string : split( )

const fruits = 'apple', 'banna', 'orange';

const result = fruits.split( )

 

 

댓글