์๋ฐ์คํฌ๋ฆฝํธ์ ๋ฉ์๋ ์ตํ๊ธฐ.
์ด๊ธฐ์ฝ๋
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Array Cardio ๐ช๐ช</title>
</head>
<body>
<p><em>Psst: have a look at the JavaScript Console</em> ๐</p>
<script>
// ## 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?
// Array.prototype.soem() // 19์ธ ์ด์์ ์ฌ๋์ด 1๋ช
์ด์.
// Array.prototype.every() // is everyone 19 or older?
// Array.prototype.every() // ๋ชจ๋ 19์ธ ์ด์์ธ๊ฐ/.
// Array.prototype.find()
// Find is like filter, but instead returns just the one you are looking for
// Find๋ ํํฐ๊ฐ์ต๋๋ค, ๊ทธ๋ฌ๋ ๋์ ์ฐพ๊ณ ์๋ ํญ๋ชฉ๋ง ๋ฐํํฉ๋๋ค.
// find the comment with the ID of 823423
// ID๊ฐ 823423์ธ comment ์ฐพ๊ธฐ.
// Array.prototype.findIndex()
// Find the comment with this ID
// ์ด ID๋ก comment ์ฐพ๊ธฐ
// delete the comment with the ID of 823423
// ID๊ฐ 823423์ธ comment๋ฅผ ์ง์ฐ๊ธฐ
</script>
</body>
</html>
some
const isAdult = people.some(function(person) {
const currentYear = (new Date()).getFullYear();
if(currentYear - person.year >= 19){
return true;
}
});
ES6 Arrow Function
const isAdult = people.some(person=>{
const currentYear = (new Date()).getFullYear();
return currentYear - person.year >= 19;
})
์ต์ข
const isAdult = people.some(person => ((new Date()).getFullYear()) - person.year >= 19);
every
const allAdults = people.every(person => ((new Date()).getFullYear()) - person.year >= 19);
const comment = comments.find(function(comment){
if(comment.id === 823423){
return true;
}
});
ES6 Arrow Function
const comment = comment.find(comment => comment.id === 823423);
const index = comments.findIndex(comment => comment.id === 823423);
console.log(index);
// comments.slice(index, 1);
const newComments=[
...comments.splice(0,index),
...comments.splice(index+1),
];
[comments]
[newComments]
... ์ ์ฌ์ฉํด newComments์ id๊ฐ 823423์ธ ๊ฐ์ ๋บ ๋๋จธ์ง๋ฅผ ๋ฃ๋ ์ฝ๋.
slice()
: ์์๊ณผ ๋์ ์ง์ ํ์ฌ ๋ฐฐ์ด์์๋ฅผ ์ถ์ถ
splice()
: ํน์ ์์น์ ์์๋ฅผ ์ญ์ ํ๊ฑฐ๋ ์์ ํ ์ ์์ต๋๋ค.
๋ฐฐ์ด.splice(4,1) >> 4๋ฒ ์ธ๋ฑ์ค๋ถํฐ 1๊ฐ ์ญ์