๐Ÿค” some() & every()?

gunwooko

Gunwoo

Posted on April 4, 2021

๐Ÿค” some() & every()?

๐Ÿ“ ๊ถ๊ธˆํ•œ ๋ชจ๋“  ๊ฒƒ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
๐Ÿ˜Ž ๊ธฐ๋ก์—์„œ ๋ฉˆ์ถ”์ง€ ์•Š๊ณ  ๋‚˜์˜ ๊ฒƒ์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
๐Ÿ™ˆ ์ž˜๋ชป๋œ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ ๋Œ“๊ธ€์— ๋‚จ๊ฒจ์ฃผ์„ธ์š” :D

๐Ÿค” some()?

some ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด ์•ˆ์˜ ์–ด๋–ค ์š”์†Œ๋ผ๋„ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•œ๋‹ค.
์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๊ฒ€์‚ฌํ•ด์„œ ์ฐธ์ธ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค.
๊ทธ๋ฆฌ๊ณ  some ๋ฉ”์„œ๋“œ์—์„œ๋Š” ๋‹จ ํ•˜๋‚˜์˜ ์š”์†Œ๋ผ๋„ ์ฝœ๋ฐฑํ•จ์ˆ˜์—์„œ ์ฐธ์ธ ๊ฐ’์ด๋ผ๋ฉด, ์ฆ‰์‹œ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ˆœํšŒ๊ฐ€ ์ค‘๋‹จ๋œ๋‹ค. ๋ชจ๋“  ๊ฐ’์ด ๊ฑฐ์ง“์ด๋ผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));
// expected output: true
Enter fullscreen mode Exit fullscreen mode

๐Ÿค” every()?

every ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด ์•ˆ์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•œ๋‹ค.
every ๋ฉ”์„œ๋“œ๋Š” ์ฝœ๋ฐฑ์ด ๊ฑฐ์ง“์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ์ฆ‰์‹œ false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ˆœํšŒ๊ฐ€ ์ค‘๋‹จ๋œ๋‹ค. ๋ชจ๋“  ๊ฐ’์ด ์ฐธ์ด๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// expected output: true
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘€ References

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
gunwooko
Gunwoo

Posted on April 4, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About