[JS] Array

Janet·2022년 9월 1일
0

JavaScript

목록 보기
9/26
// Array

// 0. 배열 만들기
const array1 = [a, b, c, d, e];
const array2 = new Array(a, b, c, d, e);


// 1. Index position
const fruits = [apple, banana, orange];
console.log(fruits[0]); // apple
console.log(fruits[1]); // banana
console.log(fruits[fruits.length - 1]); // orange
// array.length - 1 을 통해 배열의 마지막 index에 접근 가능


// 2. Looping over an array -> print all fruits
// a. for
console.log(fruits); // (3) ["apple", "banana", "orange"]
for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]); // apple // banana // orange
}
// b. for...of
for (let fruit of fruits) {
    console.log(fruit);  // apple // banana // orange
}
// c. forEach
fruits.forEach((fruit, index) => console.log(fruit, index)); // apple 0 // banana 1 // orange 2


// 3. Addition, deletion, copy
// push: add an item to the end
console.log(fruits); // (3) ["apple", "banana", "orange"]
fruits.push("melon", "peach");
console.log(fruits); // (5) ["apple", "banana", "orange", "melon", "peach"]

// pop: remove an item from the end
console.log(fruits); // (5) ["apple", "banana", "orange", "melon", "peach"]
fruits.pop(); // peach
console.log(fruits); // (4) ["apple", "banana", "orange", "melon"]
const popped = fruits.pop(); // pop할 데이터를 변수로 할당할 수 있음
console.log(popped); // melon
console.log(fruits); // (3) ["apple", "banana", "orange"]


// unshift: add an item to the begining
console.log(fruits); // (4) ["apple", "banana", "orange", "melon"]
fruits.unshift("kiwi");
console.log(fruits); // (5) ["kiwi", "apple", "banana", "orange", "melon"]

// shift: remove an item from the begining
console.log(fruits); // (5) ["kiwi", "apple", "banana", "orange", "melon"]
fruits.shift();
console.log(fruits); // (4) ["apple", "banana", "orange", "melon"]

// **note: shift, unshift는 pop과 push보다 연산이 느림
// -> 배열의 전체 데이터가 움직여야하는 동작이 요구되기때문에 배열의 길이가 길면 길수록 느려질 수 밖에.

// splice: remove an item by index position
// splice(지우기를 시작할 index number, 지울 갯수?) -> 두번째 인자는 선택사항
console.log(fruits); // (4) ["apple", "banana", "orange", "melon"]
fruits.splice(0, 1); // (3) ["banana", "orange", "melon"]
fruits.splice(1); // (1) ["orange"] // 두번째 인자를 미지정했기에 index 1번만 남기고 모두 삭제
fruits.splice(0, 1, "apple", "lemon"); // (2) ["apple", "lemon"] // 0번째 인덱스 1개 삭제 후 새로운 값 2개 추가

// concat: combine two arrays (concatenate: 사슬같이 잇다; 연쇄시키다; <사건 등을> 결부[연결]시키다, 연관시키다)
console.log(fruits); // (2) ["apple", "lemon"]
const fruits2 = ["peach", "pineapple"];
const newFruits = fruits.concat(fruits2);
console.log(newFruits); // (4) ["apple", "lemon", "peach", "pineapple"]


// 4. Serching
// indexOf: find the index
console.log(fruits); // (4) ["apple", "lemon", "peach", "pineapple"]
console.log(fruits.indexOf("peach")); // 2
console.log(fruits.indexOf("melon")); // -1 // 해당 배열에 없는 값이기에 index: -1 출력

// include: array에 포함된 데이터인지 true/false 출력
console.log(fruits.includes("melon")); // false

// lastIndexOf: array에 중복된 데이터 있을 경우, 배열에서 마지막 데이터의 index 출력
console.log(fruits); // (4) ["apple", "lemon", "peach", "pineapple"]
fruits.push("apple");
console.log(fruits); // (5) ["apple", "lemon", "peach", "pineapple", "apple"]
console.log(fruits.indexOf("apple")); // 0 
console.log(fruits.lastIndexOf("apple")); // 4
profile
😸

0개의 댓글