본문 바로가기
develop/javascript

[JS] 2차열 배열

by hyoE 2021. 6. 24.
반응형

 

 

2차원 배열이란?

배열 안에 또 다른 배열이 존재하는 것.

JS는 모든 것이 객체이기 때문에 배열의 각 요소를 다시 배열로 정의해 중첩하는 식으로 배열을 구현한다.

 

Example

let test = [['apple', 2], ['banana', 5], ['mango', 3], ['mandarine', 1]];
    console.log(test[0][0]); // apple

test[0] = ['apple', 2] -> ['apple', 2][0] = 'apple'

 

    test.push(['grape', 7]);
    console.table(test); 
    // test = [['apple', 2], ['banana', 5], ['mango', 3], ['mandarine', 1], ['grape', 7]];
    
    test.unshift(['grape', 7]);
    console.table(test); 
    // test = [['grape', 7], ['apple', 2, ['banana', 5], ['mango', 3], ['mandarine', 1]]];
  
    
    test.pop();
    console.table(test); 
    // test = [['apple', 2], ['banana', 5], ['mango', 3]];

    test.shift();
    console.table(test); 
    // test = [['banana', 5], ['mango', 3], ['mandarine', 1]];

 

test.push() -> 맨 뒤에 배열 추가

test.unshift() -> 맨 앞에 배열 추가

test.pop() -> 맨 뒤에 배열 제거

test.shift() -> 맨 앞에 배열 제거

 

반응형

'develop > javascript' 카테고리의 다른 글

[JS] filter  (0) 2021.06.28
[JS] sort  (0) 2021.06.27
[JS] for in / for of 그리고 map / reduce  (0) 2021.06.26
[JS] slice & splic  (0) 2021.06.25
[JS] D-day 만들기  (0) 2021.06.09

댓글