๐ split : String โ Array
{
const fruits = '๐, ๐ฅ, ๐, ๐';
console.log(fruits.split(','));
console.log(fruits.split(',', 2)); // ๋ฆฌํด ๋ฐ์ ๋ฐฐ์ด ์
}
๐ join : Array โ String
{
const fruits = ['apple', 'banana', 'orange'];
console.log(fruits.join()); // apple,banana,orange
console.log(fruits.join('|')); // apple|banana|orange
}
๐ Reverse
{
const array = [1, 2, 3, 4, 5];
console.log(array.reverse()); //๋ณธ ๋ฐฐ์ด๋ ์ ๋ ฌ๋จ.
}
๊ฒฐ๊ณผ๊ฐ: [5, 4, 3, 2, 1]
๐ Slice : ์์ ๋ณต์ฌ
- ์ง์ ํ ๋ฒ์๋ฅผ ์๋ก์ด ๋ฐฐ์ด๊ฐ์ฒด๋ก return (์๋ณธ ๋ฐฐ์ด ์์ X)
make new array without the first two elements
{
const array = [1, 2, 3, 4, 5];
console.log(array.slice(2, 5)); // ๋ฐฐ์ด์์ ์ํ๋ ๋ถ๋ถ๋ง return
}
// ๊ฒฐ๊ณผ๊ฐ [3, 4, 5]
// array = [1, 2, 3, 4, 5]
๐ Splice : ๋ฐฐ์ด ๊ธฐ์กด ์์ ์์
- ๋ฐฐ์ด์ ๊ธฐ์กด ์์๋ฅผ ์ญ์ ๋๋ ๊ต์ฒดํ๊ฑฐ๋ ์ ์์๋ฅผ ์ถ๊ฐํ์ฌ ๋ฐฐ์ด์ ๋ด์ฉ์ ๋ณ๊ฒฝ
- ์๋ณธ ๋ฐฐ์ด ๋ณ๊ฒฝ!
{
const array = [1, 2, 3, 4, 5];
console.log(array.splice(0, 2)); // ๋ณธ ๋ฐฐ์ด์์ 1,2๋ฅผ ์ญ์
}
// ๊ฒฐ๊ณผ๊ฐ [1, 2]
// array = [3, 4, 5]
๐ 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 a student with the score 90
{
students.find((student) => student.score === 90);
}
๐ filter : ์กฐ๊ฑด์ด true์ธ ์์๋ค์ ๋ชจ์์ ์๋ก์ด ๋ฐฐ์ด๋ก ๋ฆฌํด
- ์๋ก์ด ๋ฐฐ์ด๊ฐ์ฒด๋ก return
// make an array of enrolled students
{
console.log(students.filter((student) => student.enrolled === true));
}
๐ map
- ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ค์ ์ฒ์๋ถํฐ ๋๊น์ง ํ๋์ฉ ๊บผ๋ด์ด callbackํจ์๋ฅผ ํธ์ถํ๊ณ , callbackํจ์์์ ๋ฐํ๋์ด์ง ๊ฐ๋ค๋ก ์๋ก์ด ๋ฐฐ์ด์ ๋ฆฌํด
// make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
console.log(students.map((student) => student.score));
}
๐ some & every
- some
:๋ฐฐ์ด์ ์์ ์ค์์ callbackํจ์์ ๋ฆฌํด ์กฐ๊ฑด์ด ํ๋๋ผ๋ true์ด๋ฉด ture๋ฅผ ๋ฐํ (OR)
- every
:๋ฐฐ์ด์ ๋ค์ด์๋ ๋ชจ๋ ์์๋ค์ด ๋ฆฌํด ์กฐ๊ฑด์ ์ถฉ์กฑํ๋ฉด true, ์๋๋ฉด false (AND)
๋ชจ๋ ๋ฐฐ์ด์ ์กฐ๊ฑด์ด ๋ง์กฑ๋์ผํ ๋ ์ธ ๊ฒ
// check if there is a student with the score lower than 50
{
// some()
const result = students.some((student) => student.score < 50);
// every()
const result2 = !students.every((student) => student.score >= 50);
console.log(result);
}
๐ reduce : ๋ฐฐ์ด ๋ชจ๋ ์์๋ฅผ ๋๋ฉด์ ๋์ ํ ๋ ์ฐ๋ API
// compute students' average score
{
// my code
let temp = 0;
students.forEach((student) => (temp += student.score));
console.log(temp / students.length);
// answer
const result = students.reduce(function (prev, curr) {
return prev + curr.score;
}, 0);
console.log(result / students.length);
}
๐ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ : map + filter + join
// make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
{
// ํจ์ํ ํ๋ก๊ทธ๋๋ฐ
const result = students
.map((student) => student.score)
.filter((score) => score > 50)
.join();
console.log(result);
}
๐ Ascending order
// Bonus! do sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
console.log(
students
.map((student) => student.score)
.sort((a, b) => b - a) // desc
.sort((a, b) => a - b) // asc
.join()
);
}
์ฐธ๊ณ
๐๋๋ฆผ์ฝ๋ฉ ์๋ฆฌ๋์ youtube๊ฐ์๋ฅผ ๋ณด๊ณ ๊ณต๋ถํ ๋ด์ฉ์ ์ ๋ฆฌํ ํฌ์คํ ์ ๋๋ค๐
๐youtube๊ฐ์ https://www.youtube.com/watch?v=3CUjtKJ7PJg&list=PLv2d7VI9OotTVOL4QmPfvJWPJvkmv6h-2&index=9
'์ธ์ด > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TIL01 | JavaScrpit ๋ณ์ (0) | 2021.09.26 |
---|---|
[JS] Dynamic typing (dynamic type vs static type) (0) | 2021.07.10 |
[JS] undefined์ null (0) | 2021.07.10 |
[JS] async function (0) | 2021.07.08 |
[JS] Promise ๊ฐ๋ ๋ฐ ํ์ฉ (0) | 2021.07.06 |