console.log(Math.floor(5.95)); // 5
console.log(Math.floor(5.05)); // 5
console.log(Math.floor(5)); // 5
console.log(Math.floor(-5.05)); // -6
Math.ceil(.95); // 1
Math.ceil(4); // 4
Math.ceil(7.004); // 8
Math.ceil(-0.95); // -0
Math.ceil(-4); // -4
Math.ceil(-7.004); // -7
const array1 = [1, 4, 9, 16];
const map1 = array1.map((x) => x * 2);
console.log(map1); // Array [2, 8, 18, 32]
array.sort(function(a, b){return a - b}); // 오름차순
array.sort((a, b) => a - b)
sort()값을 비교 함수로 보내고 반환된(음수, 0, 양수) 값에 따라 값을 정렬합니다.(a - b)일 떄 (40 - 100)을 계산하면 결과가 음수(-60)이므로array.sort((a, b) => b - a) // 내림차순
const numberArr = [1, 2, 3, 4, 5];
const numberFilterArr = numberArr.filter((item) => {
return item % 2 === 0; // 해당조건에 부합으면 item을 넣어 배열 반환
});
console.log(numberFilterArr); // [2, 4]
const arr = [1, 2, 3, 4, 5];
const result = arr.reduce((acc, cur) => {
return (acc += cur);
}, 0);
console.log(result); // 15
const arr2 = [1, 2, 3, 4, 5];
const result2 = arr2.reduce((acc, cur) => {
return (acc += cur);
}, 10);
console.log(result2); // 25
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
console.log(words[3]); // "fox"
const chars = str.split('');
console.log(chars[8]); // "k"
const strCopy = str.split();
console.log(strCopy); // Array ["The quick brown fox jumps over the lazy dog."]
const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join()); // "Fire,Air,Water"
console.log(elements.join('')); // "FireAirWater"
console.log(elements.join('-')); // "Fire-Air-Water"
begin 인덱스부터 end 인덱스까지(end 미포함)에 대한 얕은 복사본을 새로운 배열 객체로 반환합니다.// slice(시작인덱스, end인덱스-미포함)
const fruits = ['🍎', '🍓', '🍌', '🍊', '🍇'];
// 2번 인덱스부터
console.log(fruits.slice(2)); // [ '🍌', '🍊', '🍇' ]
// 2번 인덱스부터 4번 인덱스 이전까지
console.log(fruits.slice(2, 4)); // [ '🍌', '🍊' ]
// 1번 인덱스부터 5번 인덱스 이전까지
console.log(fruits.slice(1, 5)); // [ '🍓', '🍌', '🍊', '🍇' ]
// 뒤에서 2 인덱스까지
console.log(fruits.slice(-2)); // [ '🍊', '🍇' ]
// 2번 인덱스부터 뒤에서 1 인덱스까지
console.log(fruits.slice(2, -1)); // [ '🍌', '🍊' ]
console.log(fruits.slice()); // [ '🍎', '🍓', '🍌', '🍊', '🍇' ]
// array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
// 📌 splice(시작인덱스, 제거할 요소의 수, 배열에 추가할 요소)
// - deleteCount : 0 이하라면 어떤 요소도 제거X (optional)
// - item1, item2, ... : 배열에 추가할 요소를 생략하면 제거만 수행 (optional)
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb'); // 1번 인덱스에 삽입
console.log(months); // [ 'Jan', 'Feb', 'March', 'April', 'June' ]
months.splice(4, 1, 'May'); // 4번 인덱스에 1개 요소 대체
console.log(months); // [ 'Jan', 'Feb', 'March', 'April', 'May' ]
유사 배열 객체란, 키가 인덱스 값으로 되어있고 길이를 나타내는 length 속성을 갖는 객체를 의미// 1. 문자열을 배열로 만드는 예시
console.log(Array.from("Hello"));
// [ 'H', 'e', 'l', 'l', 'o' ]
// 2. 유사 배열 객체를 배열로 만드는 예시
console.log(Array.from({ 0: "찬민", 1: "희진", 2: "태인", length: 3 }));
// [ '찬민', '희진', '태인' ]
// 3. 함수의 매개변수들을 순서대로 배열로 만드는 예시
const funcA = (...arguments) => {
return Array.from(arguments)
}
console.log(funcA(1,2,3,4,5));
// [ 1, 2, 3, 4, 5 ]
Array.from() 의 첫 번째 인자는 배열로 만들 이터러블한 객체가 되며, 두 번째 인자는 생성한 배열의 모든 원소에 대해 수행할 맵핑 함수입니다. (Array.map() 이라고 생각하시면 됩니다.)
한번 Array.from()과 반복문을 활용해 1부터 31까지의 수를 원소로 갖는 배열을 생성해 보겠습니다.
// 맵핑 함수의 첫 번째 인자 언더스코어(_) 는 특별한 인자가 아니라,
// 불필요한 인자의 공간을 채우기 위한 용도입니다.
const arr = Array.from(Array(31), (_, index) => index + 1);
console.log(arr);
const array1 = [1, 2, 3];
console.log(array1.includes(2)); // true
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat')); // true
console.log(pets.includes('at')); // false
// arr.indexOf(searchElement[, fromIndex])
// arr.indexOf(배열에서 찾을 요소, 검색을 시작할 색인-option)
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison')); // 1
console.log(beasts.indexOf('bison', 2)); // 4
console.log(beasts.indexOf('giraffe')); // -1
const p = 'lazy dog. If the dog reacted, was it really lazy?';
// 📝 1. replace() : 첫 번쨰 해당하는 문자열만 치환
console.log(p.replace('dog', 'monkey'));
// lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i; // 정규표현식도 사용 가능
console.log(p.replace(regex, 'ferret'));
// lazy ferret. If the dog reacted, was it really lazy?"
// 📝 2. replaceAll() : 해당하는 모든 문자열 치환
console.log(p.replaceAll('dog', 'monkey'));
// lazy monkey. If the monkey reacted, was it really lazy?"
const regex = /Dog/ig;
console.log(p.replaceAll(regex, 'ferret'));
// lazy ferret. If the ferret reacted, was it really lazy?"
substring(시작 인덱스, 끝 인덱스-미포함)const str = 'Mozilla';
console.log(str.substring(1, 3)); // "oz"
console.log(str.substring(2)); // "zilla"