순회
a = [1, 3, 45, 2, 10];
a.forEach((e, i) => {
console.log(e, i);
});
// 1 0
// 3 1
// 45 2
// 2 3
// 10 4
JavaScript
복사
문자열 분할, 합체
const str = "Hello World";
const ret = str.split(" ");
console.log(ret); // ['Hello', 'World']
const a = ret.join(" ");
console.log(a); // Hello World
JavaScript
복사
정렬
let numbers = [3, 1, 4, 1, 4, 11, 2, 3, 6, 5, 8];
numbers = numbers.sort((a, b) => a - b); //오름차순
console.log(numbers); // [1, 1, 2, 3, 3, 4, 4, 5, 6, 8, 11]
let numbers = [3, 1, 4, 1, 4, 11, 2, 3, 6, 5, 8];
numbers = numbers.sort((a, b) => a - b * -1); //내림차순
console.log(numbers); // [11, 8, 6, 5, 4, 4, 3, 3, 2, 1, 1]
JavaScript
복사
필터링
const numbers = [1, 2, 3, 4, 5, 6];
const ret = numbers.filter((e) => e % 2 == 0);
console.log(ret);
JavaScript
복사
Map
const numbers = [1, 2, 3, 4, 5, 6];
const ret = numbers.map((e) => e * e);
console.log(ret); // [1, 4, 9, 16, 25, 36]
JavaScript
복사