[JS] ๐๏ธJavaScript 30 - Day 7
๐Day 7 - Array Cardio Day 2
JavaScript 30์ Day 7 ํ๋ก์ ํธ๋ Day 4์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฌด์ธ๊ฐ ๋ง๋๋๊ฒ ์๋ JavaScript์ Array์ ๋ํด์ ๋ ์์๋ณด๋ ํ๋ก์ ํธ์ด๋ค. ์ ๋ฒ์ ๋ฐฐ์ด ๊ฒ๊ณผ ๋ค๋ฅธ method๋ค์ ๊ณต๋ถํด๋ณด์.
๐ค๐์ฝ๋ ๋ชจ์๋ณด๊ธฐ
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Array Cardio ๐ช๐ช</title>
</head>
<body>
<p><em>Psst: have a look at the JavaScript Console</em> ๐</p>
<script src="main.js"></script>
</body>
</html>
JavaScript
// ## Array Cardio Day 2
const people = [
{ name: "Wes", year: 1988 },
{ name: "Kait", year: 1986 },
{ name: "Irv", year: 1970 },
{ name: "Lux", year: 2015 },
];
const comments = [
{ text: "Love this!", id: 523423 },
{ text: "Super good", id: 823423 },
{ text: "You are the best", id: 2039842 },
{ text: "Ramen is my fav food ever", id: 123523 },
{ text: "Nice Nice Nice!", id: 542328 },
];
// Some and Every Checks
// Array.prototype.some() // is at least one person 19 or older?
const isAdult = people.some(function (person) {
const currentYear = new Date().getFullYear();
if (currentYear - person.year >= 19) {
return true;
}
});
// Arrow function
// const isAdult = people.some(
// (person) => new Date().getFullYear() - person.year >= 19
// );
console.log({ isAdult });
// Array.prototype.every() // is everyone 19 or older?
const allAdults = people.every(function (person) {
const currentYear = new Date().getFullYear();
if (currentYear - person.year >= 19) {
return true;
}
});
console.log({ allAdults });
// Array.prototype.find()
// Find is like filter, but instead returns just the one you are looking for
// find the comment with the ID of 823423
// const comment = comments.find(function (comment) {
// if (comment.id === 823423) {
// return true;
// }
// });
// Arrow function
const comment = comments.find((comment) => comment.id === 823423);
console.log(comment);
// Array.prototype.findIndex()
// Find the comment with this ID
// delete the comment with the ID of 823423
const index = comments.findIndex((comment) => comment.id === 823423);
console.log(index);
// comments.splice(index, 1);
const newComments = [...comments.slice(0, index), ...comments.slice(index + 1)];
๐์ฝ๋ ์ค๋ช
Array.some()
some()
๋ฉ์๋๋ callback ํจ์๊ฐ ์ฐธ(๋ถ๋ฆฐ์ผ๋ก ๋ณํํ์ ๋ true๊ฐ ๋๋ ๊ฐ)์ ๋ฐํํ๋ ์์๋ฅผ ์ฐพ์ ๋๊น์ง ๋ฐฐ์ด์ ์๋ ๊ฐ ์์์ ๋ํด ํ ๋ฒ์ฉ callback ํจ์๋ฅผ ์คํํ๋ค. ํด๋นํ๋ ์์๋ฅผ ๋ฐ๊ฒฌํ ๊ฒฝ์ฐ some์ ์ฆ์ true๋ฅผ ๋ฐํํ๊ณ ๊ทธ๋ ์ง ์์ผ๋ฉด, ์ฆ ๋ชจ๋ ๊ฐ์์ ๊ฑฐ์ง์ ๋ฐํํ๋ฉด false๋ฅผ ๋ฐํํ๋ค. ํ ๋นํ ๊ฐ์ด ์๋ ๋ฐฐ์ด์ ์ธ๋ฑ์ค์์๋ง callback์ ํธ์ถํ๊ณ , ์ญ์ ํ๊ฑฐ๋ ๊ฐ์ ํ ๋นํ ์ ์ด ์๋ ์ธ๋ฑ์ค์์๋ ํธ์ถํ์ง ์๋๋ค. ์ฆ, ๋น ๋ฐฐ์ด์์ ํธ์ถํ๋ฉด ๋ฌด์กฐ๊ฑด false๋ฅผ ๋ฐํํ๋ค. ๋ง์ง๋ง์ผ๋ก some
์ ํธ์ถํ ๋ฐฐ์ด์ ๋ณํํ์ง๋ ์๋๋ค.
function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
Array.every()
every()
๋ฉ์๋๋ callback ํจ์๊ฐ ๊ฑฐ์ง์ ๋ฐํํ๋ ์์๋ฅผ ์ฐพ์ ๋๊น์ง ๋ฐฐ์ด์ ์๋ ๊ฐ ์์์ ๋ํด ํ ๋ฒ์ฉ callbackFn ํจ์๋ฅผ ์คํํ๋ค. ํด๋นํ๋ ์์๋ฅผ ๋ฐ๊ฒฌํ ๊ฒฝ์ฐ every๋ ์ฆ์ false๋ฅผ ๋ฐํํ๊ณ ๊ทธ๋ ์ง ์์ผ๋ฉด, ์ฆ ๋ชจ๋ ๊ฐ์์ ์ฐธ์ ๋ฐํํ๋ฉด true๋ฅผ ๋ฐํํ๋ค. some
๊ณผ ๋ฌ๋ฆฌ, ๋น ๋ฐฐ์ด์์ ํธ์ถํ๋ฉด ๋ฌด์กฐ๊ฑด true๋ฅผ ๋ฐํํ๋ค. some
๊ณผ ๊ฐ์ ์ ์ every
์ญ์ ํธ์ถํ ๋ฐฐ์ด์ ๋ณํํ์ง๋ ์๋๋ค.
function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
Array.find()
find()
๋ฉ์๋๋ callback ํจ์๊ฐ ์ฐธ์ ๋ฐํ ํ ๋๊น์ง ํด๋น ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด์ callback ํจ์๋ฅผ ์คํํ๋ค. ๋ง์ฝ ํน์ ์์๋ฅผ ์ฐพ์๋ค๋ฉด find ๋ฉ์๋๋ ํด๋น ์์์ ๊ฐ์ ์ฆ์ ๋ฐํํ๊ณ , ๊ทธ๋ ์ง ์์๋ค๋ฉด undefined๋ฅผ ๋ฐํํ๋ค.
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function findCherries(fruit) {
return fruit.name === 'cherries';
}
console.log(inventory.find(findCherries)); // { name: 'cherries', quantity: 5 }
Array.findIndex()
findIndex()
๋ฉ์๋๋ callback ํจ์๊ฐ ์ฐธ์ ๋ฐํ ํ ๋๊น์ง ํด๋น ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด์ callback ํจ์๋ฅผ ์คํํ๋ค. ๋ง์ฝ ํน์ ์์๋ฅผ ์ฐพ์๋ค๋ฉด find ๋ฉ์๋๋ ํด๋น ์์์ index๋ฅผ ์ฆ์ ๋ฐํํ๊ณ , ๊ทธ๋ ์ง ์์๋ค๋ฉด -1์ ๋ฐํํ๋ค.
function isPrime(element, index, array) {
let start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) {
return false;
}
}
return element > 1;
}
console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found
console.log([4, 6, 7, 12].findIndex(isPrime)); // 2
๐TIL(Today I Learned)
- Day 4์ ๋ํด์ JavaScript์์ Array์ ๋ค์ํ method๋ค์ ๋ ์์๋ณผ ์ ์์๋ค.
some()
,every()
,fint()
,findIndex()
'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] ๐๏ธJavaScript 30 - Day 9 (0) | 2021.11.23 |
---|---|
[JS] ๐๏ธJavaScript 30 - Day 8 (0) | 2021.11.22 |
[JS] ๐๏ธJavaScript 30 - Day 6 (0) | 2021.11.19 |
[JS] ๐๏ธJavaScript 30 - Day 5 (0) | 2021.11.18 |
[JS] ๐๏ธJavaScript 30 - Day 4 (0) | 2021.11.17 |
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋ค๋ฅธ ๊ธ
-
[JS] ๐๏ธJavaScript 30 - Day 9
[JS] ๐๏ธJavaScript 30 - Day 9
2021.11.23 -
[JS] ๐๏ธJavaScript 30 - Day 8
[JS] ๐๏ธJavaScript 30 - Day 8
2021.11.22 -
[JS] ๐๏ธJavaScript 30 - Day 6
[JS] ๐๏ธJavaScript 30 - Day 6
2021.11.19 -
[JS] ๐๏ธJavaScript 30 - Day 5
[JS] ๐๏ธJavaScript 30 - Day 5
2021.11.18