상세 컨텐츠

본문 제목

배열_Array

JS

by RRS_SH 2023. 3. 1. 23:19

본문

이 JavaScript는 드림코딩 자바스크립트 기초강의를 보며 정리한 자료 입니다.

https://www.youtube.com/@dream-coding 

 

https://www.youtube.com/@dream-coding

 

www.youtube.com


 

배열은 같은 타입 데이터의 집합이다.

 

 

Declaration(선언)

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

 

Index position

const fruits = ['🍌', '🍎'];

console.log(fruits); // ['🍌', '🍎']
console.log(fruits.length); // 2
console.log(fruits[0]); // 🍌
console.log(fruits[1]); // 🍎
console.log(fruits[2]); // undefined
console.log(fruits[fruits.length - 1]); // 🍌

 

forEach

forEach(callbackfn: value, index, array => void);
// 펑션을 전달하여 사용할 수 있고, 데이터/인덱스/배열을 받아올 수 있다.

fruits.forEach(function (fruit, index, fruits) {
	console.log(any);
});

fruits.forEach((fruit) => console.log(any));

 

add, delete

// 배열 맨 뒤에 데이터를 넣는다.
fruits.push();

// 배열 맨 뒤에 데이터를 추출한다.
fruits.pop();

// 배열 맨 앞에 데이터를 넣는다.
fruits.unshift();

// 배열 맨 앞에 데이터를 추출한다.
fruits.shift();

// 지정된 index부터 지정한 갯수의 데이터를 삭제한다.
fruits.splice(1,3);

// 데이터를 삭제한 후 데이터를 추가 할 수 있다.
fruits.splice(1,3,🫐,🍋);

 

unshift, shift를 사용하면 기존에 존재하던 데이터들의 이동이 발생 하기 때문에 시간이 오래걸린다.

 

'JS' 카테고리의 다른 글

Object  (0) 2023.02.20
Function  (0) 2023.02.18
Operator  (0) 2023.02.18
Class  (0) 2023.02.16
Variable  (0) 2023.02.15

관련글 더보기