배열(Array) 정의 및 사용법

배열

1. 선언(Declaration)

const arr1 = new Array();
const arr2 = [1,2];

 

2. Index Position

const fruits = ['AA', 'BB'];
console.log(fruits); // ["AA", "BB"]
console.log(fruits[0]); // AA

 

3. 순회 및 검색 (Looping over an array)

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

// of
for(let fruit of fruits){
    console.log(fruit);
}

// forEach
fruits.forEach(function(fruit, index, array ){
    console.log(fruit, index); // 값, index를 출력
    //AA 0 
    //BB 1
});

// forEach(함수형으로 구현)
fruits.forEach( (fruit, index) => console.log(fruit, index) );

 

4. 추가, 삭제, 복사

fruits.push('CC', 'DD');
console.log(fruits);
fruits.pop(); // 맨뒤에 있는 값을 뺀다

fruits.shift(); // 맨 앞의 값을 뺀다
  • shift, unshift are slower than pop, push. 즉, shift, unshift는 pop. push에 비해서 성능이 좋지 않다.

 

기타 함수

  • includes , idnexOf , lastIndexOf

'📕 Programing > HTML-CSS' 카테고리의 다른 글

[VS Code] 원하는 서버 브라우저 설정  (0) 2021.02.08
JSON (JavaScript Object Notation) 사용법 👍  (0) 2020.12.03
콜백함수(CallBack)  (0) 2020.12.02
객체(Object) 사용법  (0) 2020.12.02
IE 문서모드 설정  (0) 2020.07.29

댓글

Designed by JB FACTORY