(JS 달리기반 강의 2회차 정리)
JS에서 배열 메서드는 크게 변경 메서드와 비변경 메서드로 나눌 수 있다.
변경 메서드는 배열 자체를 변경하는 메서드이다. 원본 배열을 수정하기 때문에 주의해서 사용해야 한다.
let fruits = ['Apple', 'Banana'];
fruits.push('Cherry');
console.log(fruits); // ['Apple', 'Banana', 'Cherry']
let fruits = ['Apple', 'Banana', 'Cherry'];
let lastFruit = fruits.pop();
console.log(fruits); // ['Apple', 'Banana']
console.log(lastFruit); // 'Cherry'
let fruits = ['Apple', 'Banana', 'Cherry'];
let firstFruit = fruits.shift();
console.log(fruits); // ['Banana', 'Cherry']
console.log(firstFruit); // 'Apple'
let fruits = ['Banana', 'Cherry'];
fruits.unshift('Apple');
console.log(fruits); // ['Apple', 'Banana', 'Cherry']
let fruits = ['Apple', 'Banana', 'Cherry'];
fruits.splice(1, 1, 'Blueberry');
console.log(fruits); // ['Apple', 'Blueberry', 'Cherry']
| compareFn(a, b) return value | sort order |
|---|---|
| > 0 | sort a after b, e.g. [b, a] |
| < 0 | sort a before b, e.g. [a, b] |
| === 0 | keep original order of a and b |
let fruits = ['Banana', 'Apple', 'Cherry'];
fruits.sort();
console.log(fruits); // ['Apple', 'Banana', 'Cherry']
let numbers = [4, 2, 5, 1, 3];
numbers.sort((a, b) => a - b); // 숫자 정렬
console.log(numbers); // [1, 2, 3, 4, 5]
비변경 메서드는 배열을 변경하지 않고, 새로운 배열을 반환하거나 값을 반환하는 메서드이다.
let fruits = ['Apple', 'Banana'];
let moreFruits = ['Cherry', 'Date'];
let allFruits = fruits.concat(moreFruits);
console.log(allFruits); // ['Apple', 'Banana', 'Cherry', 'Date']
let fruits = ['Apple', 'Banana', 'Cherry'];
let citrus = fruits.slice(1, 3);
console.log(citrus); // ['Banana', 'Cherry']
let fruits = ['Apple', 'Banana', 'Cherry'];
let fruitString = fruits.join(', ');
console.log(fruitString); // 'Apple, Banana, Cherry'
let fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits.includes('Banana')); // true
console.log(fruits.includes('Date')); // false
let fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits.indexOf('Banana')); // 1
console.log(fruits.indexOf('Date')); // -1
let numbers = [1, 2, 3];
let doubled = numbers.map(n => n * 2);
console.log(doubled); // [2, 4, 6]
let numbers = [1, 2, 3, 4, 5];
let even = numbers.filter(n => n % 2 === 0);
console.log(even); // [2, 4]
객체 메서드는 객체의 속성에 접근하거나 객체를 조작하는 다양한 기능을 제공한다.
let person = { name: 'John', age: 30, city: 'New York' };
let keys = Object.keys(person);
console.log(keys); // ['name', 'age', 'city']
let person = { name: 'John', age: 30, city: 'New York' };
let values = Object.values(person);
console.log(values); // ['John', 30, 'New York']
let person = { name: 'John', age: 30, city: 'New York' };
let entries = Object.entries(person);
console.log(entries); // [['name', 'John'], ['age', 30], ['city', 'New York']]
let target = { name: 'John' };
let source = { age: 30, city: 'New York' };
let returnedTarget = Object.assign(target, source);
console.log(target); // { name: 'John', age: 30, city: 'New York' }
console.log(returnedTarget); // { name: 'John', age: 30, city: 'New York' }
let person = { name: 'John', age: 30 };
Object.freeze(person);
person.age = 31; // 무시됨
console.log(person.age); // 30
let person = { name: 'John', age: 30 };
Object.seal(person);
person.age = 31; // 변경 가능
person.city = 'New York'; // 추가 불가
delete person.name; // 삭제 불가
console.log(person); // { name: 'John', age: 31 }