https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop());
// expected output: "tomato"
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
plants.pop();
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage"]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/push
const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
// expected output: 5
console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
const numbers = [“1”, “2”, “3”, “4”];
console.log(numbers.splice(1)); // 1번째 인덱스부터 뒷요소 모두 삭제
// [“1”]
console.log(numbers.splice(1, 1)); // 1번째 인덱스부터 1개만 삭제
// [“1”, “3”, “4”]
console.log(numbers.splice(1, 2, “5”)); // 1번째 인덱스부터 2개 삭제 후 “5” 추가
// [“1”, “5”, “4”];
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
console.log(numbers.slice()); // 0번째 인덱스부터 모두 반환(전체 복사)
// [“1”, “2”, “3”, “4”]
console.log(numbers.slice(2)); // 2번째 인덱스부터 뒷요소 모두 반환
// [“3”, “4”]
console.log(numbers.slice(1,3)); // 1번째 인덱스부터 2번째 인덱스까지 반환
// [“2”, “3”]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/split
const s = “abcd”;
console.log(s.split()); // [“abcd”]
console.log(s.split(‘’)); // [“a”, ”b”, ”c”, ”d”]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/join
const arr = [“a”, ”b”, ”c”, ”d”];
console.log(arr.join()); // “a,b,c,d”
console.log(arr.join(‘’)); // “abcd”
console.log(elements.join('-'));
// expected output: "Fire-Air-Water"
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
const array1 = ['one', 'two', 'three'];
console.log('array1:', array1);
// expected output: "array1:" Array ["one", "two", "three"]
const reversed = array1.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]
// Careful: reverse is destructive -- it changes the original array.
console.log('array1:', array1);
// expected output: "array1:" Array ["three", "two", "one"]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
const year = “2021”;
const month = “07”;
const day = “09”;
const result = year.concat(“/”, month, “/”, day);
console.log(result); // 2021/07/09
// 예시 1
let arr = [1, 2];
console.log(arr.concat([3, 4])); // [1, 2, 3, 4]
console.log(arr.concat([3, 4], [5, 6, 7])); // [1, 2, 3, 4, 5, 6, 7]
console.log(arr.concat([3, 4], 5, 9, 11)); // [1, 2, 3, 4, 5, 9, 11]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4
console.log(beasts.indexOf('giraffe'));
// expected output: -1
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// expected output: true
console.log(pets.includes('at'));
// expected output: false
const arr = ['a', 'b', 'c'];
arr.includes('c', 3); // false
arr.includes('c', 100); // false
arr.includes('a', -100); // true
arr.includes('b', -100); // true
arr.includes('c', -100); // true
arr.includes('a', -2); // false(-1 * arr.length 인 -3 보다 크므로)
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
참고) https://hianna.tistory.com/409 [어제 오늘 내일]
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]
const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]
arr.sort((a,b)=>a-b); // 오름차순 정렬
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
const array1 = [1, 2, 3, 4];
const reducer = (previousValue, currentValue) => previousValue + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
누산기 (acc), 현재 값 (cur), 현재 인덱스 (idx), 원본 배열 (src)
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/map
arr.map(callback(currentValue[, index[, array]])[, thisArg])
- currentValue : 처리할 현재 요소.
index : 처리할 현재 요소의 인덱스.
array : map()을 호출한 배열.
thisArg : callback을 실행할 때 this로 사용되는 값.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
arr.filter(callback(element[, index[, array]])[, thisArg])
- element : 처리할 현재 요소.
index : 처리할 현재 요소의 인덱스.
array : filter()을 호출한 배열.
thisArg : callback을 실행할 때 this로 사용되는 값.
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
arr.forEach(callback(currentvalue[, index[, array]])[, thisArg])
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// expected output: "a"
// expected output: "b"
// expected output: "c"
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
'abc'.repeat(-1); // 범위 에러(반복 횟수는 무한대보다 작아야 하며, 최대 문자열 크기를 넘어선 안됨.)
'abc'.repeat(0); // ''
'abc'.repeat(1); // 'abc'
'abc'.repeat(2); // 'abcabc'
'abc'.repeat(3.5); // 'abcabcabc' (반복 횟수는 양의 정수여야 함. 정수 단위로 반복함.)
'abc'.repeat(1/0); // 범위 에러
({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2);
// 'abcabc' (repeat() is a generic method)
(문자열을 배열로 - Array 메서드에서 설명)
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
const str = "pineapple is sweet";
str.indexOf("apple"); // 4
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/replace
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replaceAll('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"
// global flag required when calling replaceAll with regex
const regex = /Dog/ig;
console.log(p.replaceAll(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
// 제곱한 수를 나타내기
Math.floor(2**5); // 32
Math.pow(2, 5); // 32