๊ธ์ ์์ฑํ๊ธฐ์ ์์ ์ด ๊ฒ์๊ธ์ ๋ชจ๋ ๊ธ์ ์ ํ๋ธ ๋๋ฆผ์ฝ๋ฉ by ์๋ฆฌ๋์ ๋์์์ด ์ถ์ฒ์์ ์๋ ค๋๋ฆฝ๋๋ค.
https://www.youtube.com/watch?v=yOdAVDuHUKQ&list=PLv2d7VI9OotTVOL4QmPfvJWPJvkmv6h-2&index=8
์๋ฐ์คํฌ๋ฆฝํธ ๋ฐฐ์ด์ ์ ์ฉํ ํจ์๋ค์ ์์๋ณด์ !
Join
Join์ ๋ฐฐ์ด์ ์กด์ฌํ๋ ๋ฐ์ดํฐ๋ค์ ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ค์ด ์ฃผ๋ API์ด๋ค !
๋ฐ๋ก ์ฝ๋๋ก ์์๋ณด์
const fruits = ['apple', 'banana', 'orange'];
const result = fruits.join();
console.log(result);
์ฐ์ fruits ๋ฐฐ์ด ์์ ์๋ ์ธ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ result ๋ณ์์ ํ๋๋ก ๋ง๋ค์ด ์ฃผ๊ธฐ ์ํด join ์ ํด์ค๋ค
โ
result๋ฅผ ์ถ๋ ฅํ๋ฉด ์ด๋ป๊ฒ ๋๋์ง ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋ก ์์๋ณด์
apple,banana,orange
์ถ๋ ฅ์ด ๋ ๋ ์๋์ ์ผ๋ก ์ฝค๋ง , ๊ฐ ์ฌ์ด์ ์ถ๋ ฅ๋๋ ๊ฒ์ ์ ์ ์๋ค join์ ๊ดํธ () ์ ๊ตฌ๋ถ์๋ฅผ ๋ฃ๊ฒ ๋๋ฉด ๋ฐ์ดํฐ ์ฌ์ด์ ๋ค๋ฅธ ๊ฐ์ด ์ถ๋ ฅ๋๋ค
const result = fruits.join('|');
์ด๋ฒ์๋ ๋ฐ์ดํฐ ์ฌ์ด์ ๊ตฌ๋ถ์ | ๋ฅผ ํฌํจํ์ฌ ์ถ๋ ฅํด๋ณด์
apple|banana|orange
split
์ด๋ฒ์๋ ์ฃผ์ด์ง๋ ๋ฌธ์์ด์ ๋ฐฐ์ด๋ก ๋ง๋ค์ด์ฃผ๋ API์ด๋ค
์์๋ฅผ ํตํด ๋ฐ๋ก ์์๋ณด์ !
const fruits = 'apple, banana, orange, cherry';
const result = fruits.split(',');
console.log(result);
split์ ์ด์ฉํ์ฌ fruits ์์ ๋ฌธ์์ด์ ๋ฐฐ์ด๋ก ๋ง๋ค ์ ์๋ค
(4) ['apple','banana','orange','cherry']
์ด๋ฒ์๋ ๋ฐฐ์ด๋ก ๋ง๋ค์ด์ง๋ ๊ฐ์๊น์ง ์ง์ ํด๋ณด์
const result = fruits.split(',', 3);
์์ฒ๋ผ 3๊ฐ๋ง ๋ฐฐ์ด๋ก ๋ง๋ค์ด์ง๋๋ก ํด๋ณด์๋ค
(3) ['apple','banana','orange']
reverse
์ด๋ฒ์๋ ๋ฐฐ์ด ์์ ์์๋ฅผ ๊ฑฐ๊พธ๋ก ๋ง๋ค ์ ์๋ reverse์ด๋ค
const array = [1, 2, 3, 4, 5];
array.reverse();
console.log(array);
์์ ๊ฐ์ด reverse๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐ๋๋ก ์ถ๋ ฅ์ด ๋๋ค
(5) [5, 4, 3, 2, 1]
๋ฐฐ์ด์ ์์๊ฐ ๊ฑฐ๊พธ๋ก ๋ค๋ฐ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค
slice
๋ฐฐ์ด์ ์ง์ ๋ ๋ฐ์ดํฐ๋ฅผ ๋นผ๊ณ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํ๋ API๋ฅผ ์์๋ณด์
์ ๋ฒ ๊ฐ์์ ๋ฐฐ์ด splice๋ ๋ฐฐ์ด ์์ฒด์ ์ค์ ๋ก ์กด์ฌํ๋ ๊ฐ์ ์ญ์ ํ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉํ์ง ๋ชปํ๊ณ
โ
๋ฐฐ์ด์ ํน์ ํ ๋ถ๋ถ์ ๋ฆฌํดํ๋ slice ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค
โ
โ
1๊ณผ 2๋ฅผ ์ ์ธํ ๊ฐ๋ค์ ์ถ๋ ฅํด๋ณด์
const array = [1, 2, 3, 4, 5];
const result = array.slice(2, 5);
console.log(result);
console.log(array);
์ฌ๊ธฐ์ ์ฃผ์ํ ์ ์ slice์ () ์์ ์ฒซ ๋ฒ์งธ ์ธ์๋ ์ด๋์๋ถํฐ ์์ํ ๊ฑด์ง ์ ํด์ผ ํ๊ธฐ ๋๋ฌธ์ 2๋ฅผ ์ง์ ํด ์ฃผ์๊ณ
๋ ๋ฒ์งธ 5๋ผ๊ณ ํ ์ด์ ๋ ๋ง์ง๋ง ์ธ๋ฑ์ค๋ ๋ฐฐ์ ๊ฐ ๋๊ธฐ ๋๋ฌธ์ 4๊ฐ ์๋ 5๋ผ๊ณ ํด์ฃผ์ด์ผ ํ๋ค
(3) [3, 4, 5]; // result
(5) [1, 2, 3, 4, 5]; //array
์์์ ์ถ๋ ฅํ result๋ 2๋ฒ์งธ ์๋ฆฌ๋ถํฐ 4๋ฒ์งธ ์๋ฆฌ๊น์ง ์๋ ค์ ์ถ๋ ฅ์ด ๋์๊ณ
splice์ ๋ค๋ฅด๊ฒ ์๋ array์ ๋ฐ์ดํฐ๋ ๋ณํ๊ฐ ์๋ ๊ฒ์ ์ ์ ์๋ค
find
๋ค์์ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ์ฐพ๊ณ ์ ํ๋ ๊ฐ์ ๊ฐ๊ณ ์๋ ๊ฐ์ฒด๋ฅผ ์ฐพ๋ find์ ๋ํด ์์๋ณด์
โ
์ฐ์ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์ ์ธํด๋ณด์
class Student {
constructor(name, age, enrolled, score) {
this.name = name;
this.age = age;
this.enrolled = enrolled;
this.score = score;
}
}
const students = [
new Student('A', 29, true, 45),
new Student('B', 28, false, 80),
new Student('C', 30, true, 90),
new Student('D', 40, false, 66),
new Student('E', 18, true, 88),
];
find๋ฅผ ์ฌ์ฉํ์ฌ ์ ์๊ฐ 90์ ์ธ ํ์์ ์ฐพ์๋ณด์
const result = students.find((student) => student.score === 90);
console.log(result);
๋งค๊ฐ๋ณ์๋ก student ์๋ฆฌ๋ value์ธ๋ฐ student ๋ฐฐ์ด์ ๋ชจ๋ ํ์๋ค์ ๋ปํ๋ค!
find๋ ๋ค์ ์ฝ๋ฐฑํจ์๊ฐ ํธ์ถ์ด ๋๋๋ฐ boolean ํ์์ผ๋ก ์ํ๋ ๊ฐ์ด๋ฉด true ์๋๋ฉด false๋ก ์ฐพ๋ ๋ฐฉ์์ด๋ค
์ฆ ์์์๋ถํฐ ์์ฐจ์ ์ผ๋ก ์ ์๊ฐ 90์ธ ํ์์ ์ฐพ๋๋ค
โ
new Student('A', 29, true, 45) - score๊ฐ 45์ด๊ธฐ ๋๋ฌธ์ false
new Student('B', 28, true, 80) - score๊ฐ 80์ด๊ธฐ ๋๋ฌธ์ false
new Student('C', 30, true, 90) - score๊ฐ 90์ด๊ธฐ ๋๋ฌธ์ ์ฐพ์์ผ๋ฏ๋ก true!
โ
์ฝ๋ฐฑํจ์์ ๋ํด ๊ฐ๋ตํ ์ง๊ณ ๋์ด๊ฐ์๋ฉด ๋ง ๊ทธ๋๋ก 'Callback' ์ฆ ์ด๋ค ์กฐ๊ฑด์ ์ํด ์์ ์ ์ฒ๋ฆฌํ๋ค๊ฐ ๋์ด ๋ฌ์์ ์๋ ค์ฃผ๋ ํจ์์ด๋ค
filter
const students = [
new Student('A', 29, true, 45),
new Student('B', 28, false, 80),
new Student('C', 30, true, 90),
new Student('D', 40, false, 66),
new Student('E', 18, true, 88),
];
๋ค์์ ์์ ์ ๋ฑ๋กํ ( enrolled ) ํ์๋ค๋ง ์ฐพ์ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด ๋ด๋ filter ๋ฅผ ์ฌ์ฉํด๋ณด์
const result = students.filter((student) => student.enrolled);
์ด๋ฒ์๋ Callback ํจ์๋ก ์ฐพ๋ ๊ฒ์ด๋ค filter๋ find์ ๋ง์ฐฌ๊ฐ์ง๋ก bollean ์ผ๋ก ์ฐพ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ true๋ฅผ ์ฐพ๋๋ค enrolled ์ด ๋์๋ค๋ฉด true๋ก ์ค์ ํด ๋์์ผ๋ true์ธ ํ์๋ง ์ฐพ์ ๋ฐฐ์ด์ ๋ง๋ ๋ค!
map
const students = [
new Student('A', 29, true, 45),
new Student('B', 28, false, 80),
new Student('C', 30, true, 90),
new Student('D', 40, false, 66),
new Student('E', 18, true, 88),
];
์ด๋ฒ์๋ ํ์๋ค์ ์ ์๋ง์ผ๋ก ๋ฐฐ์ด์ ๋ง๋๋ map์ ๋ํด์ ์์๋ณด์
const result = students.map((student) => student.score);
console.log(result);
(5) [45, 80, 90, 66, 88]
map ๋ ์ฝ๋ฐฑํจ์๋ฅผ ํธ์ถํ๋ค ์ฌ๊ธฐ์ map ์ด๋ student.score๋ฅผ ์์ ๊ฐ์ด ๊ทธ๋๋ก ์ถ๋ ฅํ ์ ์๊ณ
๋๋ ๊ณฑํ๊ฑฐ๋ ๋ํ๊ธฐ ๋ฑ ๋ค๋ฅธ ์์ ๋ค์ ํด์ ์ ๋ฌํด ์ฃผ๋ mapping์ ํ๋ API์ด๋ค
const result = students.map((student) => student.score * 2);
console.log(result);
์ด๋ฒ์๋ ์ ์์ 2๋ฅผ ๊ณฑํ์ฌ mapping ํ๋ ์์ ์ ํด๋ณด์
(5) [90, 160, 180, 132, 176]
๋ง์ง๋ง์ผ๋ก ์ฝ๋ฐฑํจ์์์ item์ด๋ value์ ๊ฐ์ด ์ด๋ฆ์ ์ง์ ํ ์ ์๋๋ฐ ์์ ์ฝ๋๋ ํ ์ค ์ฝ๋์ด๊ธฐ ๋๋ฌธ์
value.score๋ผ๊ณ ํ๋ฉด ๋ฐ๋ก srudents ์์ ์๋ ๊ฐ๋ค์ score ๋ผ๊ณ ์ ์ ์์ง๋ง ์ฝ๋๊ฐ ๊ธธ์ด์ง๊ฒ ๋๋ฉด ํ๋์ ์์๋ณด๊ธฐ ์ด๋ ต๊ธฐ ๋๋ฌธ์ ๊ฐ๊ธ์ ์ด๋ฉด ์ฌ์ด ์ด๋ฆ์ ์ง๋ ๊ฒ์ด ์ค์ํ๋ค !
some, every
const students = [
new Student('A', 29, true, 45),
new Student('B', 28, false, 80),
new Student('C', 30, true, 90),
new Student('D', 40, false, 66),
new Student('E', 18, true, 88),
];
50์ ๋ณด๋ค ๋ฎ์ ํ์์ด ์๋์ง ํ์ธํ๋ some์ ์ด์ฉํ์ฌ ์ฐพ์๋ณด์
const result = students.some((student) => student.score < 50);
console.log(result);
some๋ ๋ง์ฐฌ๊ฐ์ง๋ก Callback ํจ์์ด๋ฉฐ 50์ ์ดํ์ ํ์์ด ํ ๋ช ์ด๋ผ๋ ์กด์ฌํ๋ค๋ฉด true๋ฅผ ๋ฆฌํดํ๋ค
โ
๊ฒฐ๊ณผ๋ true !
โ
๋ค์์ ๋ชจ๋ ํ์๋ค์ ์ ์๊ฐ 50์ ๋ณด๋ค ๋ฎ์์ง ๋์์ง๋ฅผ ํ์ธํ๋ every์ ๋ํด ์์๋ณด์
const result = students.every((student) => student.score < 50);
console.log(result);
๋ชจ๋ ํ์์ ์ ์๊ฐ 50์ ๋ณด๋ค ๋ฎ์ง ์์ผ๋
๊ฒฐ๊ณผ๋ false !
reduce
์ด๋ฒ์๋ ํ์๋ค์ ์ ์๋ฅผ ํ๊ท ์ ๊ตฌํ๊ธฐ ์ํด reduce์ ๋ํด ์์๋ณด์
โ
reduce๋ ํ๊ท ์ ๊ตฌํ๋ API ์๋ ๋ฐฐ์ด์ ์ฒ์๋ถํฐ ๋๊น์ง ๋๋ฉด์ ๋์ ๋ ๊ฐ์ ๊ตฌํ๋ API์ด๋ค
โ
reduce๋ ์ด์ ๊ฐ ( previous ) ์ ํ์ฌ ๊ฐ ( current ) ์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ์ด๋ ํ ๋์ ๋ ๊ฐ์ ๊ตฌํ๋ ๊ฒ์ด๋ค
์ฆ [1, 2, 3, 4] ๋ผ๋ ๋ฐฐ์ด์ด ์๋ค๊ณ ๊ฐ์ ํด๋ณด์
โ
previous ๊ฐ์ ์ด๊ธฐ๊ฐ์ 0 ์ผ๋ก ์ค์ ํ๊ณ current ๊ฐ์ ์๋์ ์ผ๋ก ์ฒซ ๋ฒ์งธ ์ธ์ ๊ฐ์ธ 1์ด ํ ๋น๋๋ค
๊ทธ ํ ๋ง์ง๋ง์ ๋ค์ previous ๊ฐ์ ๋ฆฌํดํด์ฃผ์ด์ผ ํ๋ค
โ
์ด์ ๊ฐ ( previous ) ์ ํ์ฌ ๊ฐ ( current ) ์ ๋ํด์ ๋ฆฌํดํ๋ ์ฝ๋๋ฅผ ์์ฑํ์๋ค๋ฉด
โ
previous 0
current 1
----------------
previous 1
current 2
----------------
previous 3
current 3
----------------
previous 6
current 4
----------------
๊ฒฐ๊ณผ๊ฐ 10
โ
์์ ๊ฐ์ด ๋ก์ง์ด ๋์๊ฐ๋ค
โ
previous ์๋ ๋ฆฌํด ๊ฐ์ด ๋ค์ด๊ฐ๊ณ current๋ ๋ฐฐ์ด์ ๋ฐ์ดํฐ๋ฅผ ์์ฐจ์ ์ผ๋ก ์ ๋ฌ๋ฐ๋๋ค
์ )
const students = [
new Student('A', 29, true, 45),
new Student('B', 28, false, 80),
new Student('C', 30, true, 90),
new Student('D', 40, false, 66),
new Student('E', 18, true, 88),
];
์ด๋ฅผ ์ฝ๋๋ก ์์ฑํด๋ณด๋ฉด
const result = students.reduce((prev, curr) => prev + curr.score, 0);
// prev ์ ์ด๊ธฐ๊ฐ์ 0 ์ผ๋ก ์ง์
console.log(result / students.length);
์ฐ์ prev์ curr ์ผ๋ก ์ด์ ๊ฐ๊ณผ ํ์ฌ ๊ฐ์ ๋ฐ๊ณ
์ด์ ๊ฐ๊ณผ ํ์ฌ ๊ฐ์ score ๋ฅผ ๋ํด์ฃผ๋ฉด score ๋ฅผ ๋ชจ๋ ๋ํ ๊ฐ์ด result์ ํ ๋น์ด ๋๋ค
โ
๋ฐฐ์ด์ ๋ฌธ์์ด๋ก ๋ณํ
const students = [
new Student('A', 29, true, 45),
new Student('B', 28, false, 80),
new Student('C', 30, true, 90),
new Student('D', 40, false, 66),
new Student('E', 18, true, 88),
];
ํ์๋ค์ ์ ์ ๋ฐฐ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ๋ณํํด๋ณด์
const result = students.map((student) => student.score) //score๋ง ๋ฐฐ์ด๋ก ์์ฑ
.filter((score) => score >= 50) //์ ์๊ฐ 50์ ์ด์์ธ ๋ฐฐ์ด๋ก ๊ตฌ๋ถ
.join(); //score๋ฐฐ์ด์ join ํ์ฌ ํ๋์ ๋ฌธ์์ด๋ก ๋ณํ
console.log(result);
์์ ๊ฐ์ด map์ผ๋ก score๋ฅผ mapping ํ์ฌ ๋ฐฐ์ด๋ก ์์ฑํ๊ณ
filter๋ฅผ ์ฌ์ฉํ์ฌ ์ ์๊ฐ 50์ ์ด์์ธ score๋ง ๋ฐ๋ก ๊ตฌ๋ถํ๋ค
join์ ์ด์ฉํ์ฌ ๋ฐฐ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ๋ณํ !
โ
80, 90, 66, 88
sort
const students = [
new Student('A', 29, true, 45),
new Student('B', 28, false, 80),
new Student('C', 30, true, 90),
new Student('D', 40, false, 66),
new Student('E', 18, true, 88),
];
๋ง์ง๋ง์ผ๋ก ๋ฐฐ์ด์ sort๋ก ์ ๋ ฌํ ๋ค ๋ฌธ์์ด๋ก ๋ง๋ค์ด๋ณด์
โ
const result = students
.map((student) => student.score) //score ๋ง ์กด์ฌํ๋ ๋ฐฐ์ด์ ์์ฑํ๋ค
.sort((a, b) => a - b) // ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ a - b
.join(); //join ํ์ฌ ๋ฌธ์์ด ์์ฑ
console.log(result);
์ฐ์ map์ผ๋ก score๋ง ์กด์ฌํ๋ ๋ฐฐ์ด์ ๋ง๋ ํ์
sort๋ก a ์ b์ ๊ฐ์ ๋ฐ์ a - b ๋ฅผ ํด์ค๋ค ( a - b ) ๋ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ์ด๋ค
๋ง์ง๋ง์ผ๋ก join์ผ๋ก ๋ฌธ์์ด์ ์์ฑํ๋ค
โ
๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ์ ํ ๋์๋
.sort((a, b) => b - a);
b - a ๋ผ๊ณ ํด์ฃผ๋ฉด ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ์ด ๋๋ค
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ธ๋ผ์ฐ์ ๋ ๋๋ง ์๋ฆฌ ( Critical Rendering Path ) (0) | 2021.12.01 |
---|---|
JavaScript (ES6) - ์๋ฒ ํต์ ์ ์์ JSON (0) | 2021.05.23 |
JavaScript (ES6) - Prototype (0) | 2021.05.20 |
JavaScript (ES6) - Array ๊ฐ๋ ๊ณผ APIs (0) | 2021.05.01 |
JavaScript (ES6) - Object (0) | 2021.05.01 |