본문 바로가기
Research/Javascript

Javascript_array

by RIEM 2022. 3. 20.

array

이제 데이터 구조에 대해 알아보는 시간이다. 

const mon = "mon";

const tue = "tue";

const wed = "wed";

const thu = "thu";

const fri = "fri";

const sat = "sat";

const sun = "sun";

 

const dayOfWork = [mon, tue, wed, thu, fri, sat, sun];

 

console.log(dayOfWork);

 

이건 파이썬에서 이미 배운 개념이다.

 

 

데이터들을 담아놓는 그릇이라고 생각하면 된다. 한 줄로 나란히 놓은 그릇이라서 console로 꺼낼 때도 한 줄로 나란히 꺼낼 수 있다.



인덱싱

 

console.log(dayOfWork[2]);

 

array 뒤에 [2]를 붙여주면 array의 3번째 요소만 호출해줘 라는 뜻이다.

 

 

여기서 왜 [2]가 3번째인가. 바로 가장 앞에 있는 것이 0이기 때문이다.

 

 

array.push()로 어레이에 데이터 추가하기

array에 push메소드를 사용하면 단어를 리스트에 추가할 수 있다.

 

const mon = "mon";

const tue = "tue";

const wed = "wed";

const thu = "thu";

const fri = "fri";

const sat = "sat";

const sun = "sun";

 

const dayOfWork = [mon, tue, wed, thu, fri, sat, sun];

 

dayOfWork.push("8th day");

 

console.log(dayOfWork)

 

 

8th day라는 말도 안되는 요일을 넣었다. 주8일이 되었다.

 

Array.prototype.push()

The push() method adds one or more elements to the end of an array and returns the new length of the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

 

모질라에 따르면 push() 메소드는 1개 이상의 엘리먼트들을 어레이의 끝에 추가하고 반환값으로 어레이의 새로운 길이를 알려준다고 한다.

 

 

'Research > Javascript' 카테고리의 다른 글

Javascript_function  (0) 2022.03.20
Javascript_Object  (0) 2022.03.20
Javascript_데이터 타입  (0) 2022.03.20
Javascript의 기본 개념  (0) 2022.03.20
JS_calculator code  (0) 2022.03.19

댓글