드림코딩 자바스크립트 기초강의 정리8

Dongwoo Kim·2021년 7월 16일
0

배열 제대로 알고 쓰자. 자바스크립트 배열 개념과 APIs 총정리

자료구조 : 데이터들을 담은 바구니 범위) 자료구조>오브젝트

'use strict';

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

//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.length-1);

//3. Looping over an array
//print all fruits
//a. for
for (let i = 0; i < fruits.length; i++) {
	console.log(fruits[i]);
}
//b. for of
for (let fruit of fruits) {
	console.log(fruit);
}
//c. forEach
fruits.forEach((fruit) => console.log(fruit));

//4. Addition, deletion, copy
//push: add an item to the end
//pop: remove an item from the end
//unshift: add an item to the beginning
//shift: remove an item from the beginning
//note!! shift, unshift are slower than push, pop! 앞에서 조작하려면 배열 요소를 많이 움직여야 하기 때문

//splice(시작점, 끝점, 추가데이터1, 추가데이터2...)
//arr1.concat(arr2)
//arr.includes(element) element가 포함됐는지 true, false로 알려줌
//arr.indexOf(element)
//arr.lastIndexOf(element)
profile
水滴石穿

0개의 댓글