class Coder {
constructor(name, age, enrolled, score) {
this.name = name;
this.age = age;
this.enrolled = enrolled;
this.score = score;
}
}
const coders = [
new Coder('A', 29, true, 45),
new Coder('B', 28, false, 80),
new Coder('C', 30, true, 90),
new Coder('D', 40, false, 66),
new Coder('E', 18, true, 88),
]
filter
enrolled๊ฐ true์ธ ์ฝ๋๋ง ๊ณจ๋ผ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด๋ณด์.
const result = coders.filter((coder) => coder.enrolled);
console.log(result);
// [object Array] (3)
// [
// {
// "name": "A",
// "age": 29,
// "enrolled": true,
// "score": 45
// },// [object Object]
// {
// "name": "C",
// "age": 30,
// "enrolled": true,
// "score": 90
// },// [object Object]
// {
// "name": "E",
// "age": 18,
// "enrolled": true,
// "score": 88
// }]
map
๋ฐฐ์ด ์์ ์๋ ์์๋ค์ ์ํ๋ ํจ์๋ฅผ ์ฌ์ฉํด ๋ค๋ฅธ ๋ฐฉ์์ ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค ์ ์๋ค.
score๋ง ๊ณจ๋ผ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด๋ณด์.
const result = coders.map((Coder) => Coder.score);
// [object Array] (5)
// [45,80,90,66,88]
score์ 2๋ฐฐ๋ฅผ ๊ณฑํ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด๋ณด์.
const result = coders.map((Coder) => Coder.score * 2);
// [object Array] (5)
// [90,160,180,132,176]
some
๋ฐฐ์ด ์์ ์๋ ์์๋ค ์ค ํ๋๋ผ๋ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด true๊ฐ ๋ฌ๋ค.
const result = coders.some((Coder) => Coder.score < 50);
// true
every
๋ฐฐ์ด ์์ ์๋ ์์๋ค์ด ์ ๋ถ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด true๊ฐ ๋ฌ๋ค.
const result = coders.every((Coder) => Coder.score < 50);
// false
์์ !๋ฅผ ๋ถ์ด๋ฉด ๋ฐ๋๋ก ์ถ๋ ฅํ ์ ์๋ค.
const result = !coders.every((Coder) => Coder.score < 50);
// true
์ถ์ฒ : ๋๋ฆผ์ฝ๋ฉ https://www.youtube.com/@dream-coding / MDN https://developer.mozilla.org/ko