Dev/JavaScript30

7. Array Cardio Day 2 ( JavaScript30 )

takeU 2021. 7. 8. 15:12
๋ฐ˜์‘ํ˜•

Array Cardio Day 2

๊ธฐ๋ณธ ์ฝ”๋“œ

<!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> 
      // ## 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 } 
      ];
      // 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 
      // Array.prototype.findIndex()
      // Find the comment with this ID 
      // delete the comment with the ID of 823423 
    </script>
  </body> 
</html>

 

๋ชฉํ‘œ

Array Cardio Day1์— ์ด์–ด ๊ฐ์ข… ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ตํ˜€๋ด„

 

๊ณผ์ •

  1. some()
  2. every()
  3. find()
  4. findIndex(), slice()

 

์ฝ”๋“œ ๋ถ„์„

1. some()

// Array.prototype.some() // is at least one person 19 or older?
const isAdult = people.some(person => ((new Date()).getFullYear()) - person.year >= 19);

console.log({ isAdult });

๊ธฐ๋ณธ ์ฝ”๋“œ์—์„œ ์„ ์–ธํ•œ people์— 19์‚ด ์ด์ƒ์ด ํ•œ๋ช… ์ด์ƒ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ
some()์€ ์•ž์˜ ๋ฐฐ์—ด์˜ ๋‚ด์šฉ ์ค‘ ์กฐ๊ฑด์— ๋งž๋Š”๊ฒƒ์ด ์žˆ๋‹ค๋ฉด true ์—†๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ.
๊ด„ํ˜ธ ์•ˆ์˜ ๋‚ด์šฉ์€ ํ˜„์žฌ ์‹œ๊ฐ„์—์„œ ๋„ค์ž๋ฆฌ๋กœ ํ˜„์žฌ ์—ฐ๋„๋ฅผ ์–ป์–ด ๋‚ธ ๋’ค ํƒœ์–ด๋‚œ ๋…„๋„๋ฅผ ๋บ์„ ๋•Œ 19 ์ด์ƒ์ด ์žˆ๋Š”์ง€
ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ, ์ฝ˜์†”์„ ํ†ตํ•ด ๊ฒฐ๊ณผ ํ™•์ธ

2. every()

// Array.prototype.every() // is everyone 19?
const allAdults = people.every(person => ((new Date()).getFullYear()) - person.year >= 19);

console.log({ allAdults });

๊ธฐ๋ณธ ์ฝ”๋“œ์—์„œ ์„ ์–ธํ•œ people์— ๋ชจ๋‘ 19์‚ด ์ด์ƒ์ธ์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ
every()๋Š” ์•ž์˜ ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์ด ๋ชจ๋‘ ์กฐ๊ฑด์— ๋งž๋‹ค๋ฉด true ์—†๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
๊ด„ํ˜ธ ์•ˆ์˜ ๋‚ด์šฉ์€ ์œ„์™€ ๋™์ผ, ์ฝ˜์†”์„ ํ†ตํ•ด ๊ฒฐ๊ณผ ํ™•์ธ

3. find()

// 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(comment => comment.id === 823423);

console.log(comment);

๊ธฐ๋ณธ ์ฝ”๋“œ์—์„œ ์„ ์–ธํ•œ comment์—์„œ ID๊ฐ€ 823423์ธ ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ฐพ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
๋ถ€์—ฐ์„ค๋ช…์ด ์—†์ด ์ฝ”๋“œ ์ž์ฒด๋กœ ์ดํ•ด๊ฐ€ ๊ฐ€๋Šฅ, ์ฝ˜์†”์ฐฝ์— ๊ฒฐ๊ณผ ์ถœ๋ ฅ

4. findIndex(), slice()

// 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)
];
console.log(newComments);

๊ธฐ๋ณธ ์ฝ”๋“œ์—์„œ ์„ ์–ธํ•œ comment์—์„œ ID๊ฐ€ 823423์ธ ๊ฐ’์„ ์ฐพ์•„ index๋ฅผ ๋ฐ˜ํ™˜ํ• ๋•Œ ์‚ฌ์šฉ
๋ฐฐ์—ด์•ˆ์— ...์„ ์‚ฌ์šฉํ•ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด newComments์•ˆ์— id๊ฐ€ 823423์ธ ๊ฐ’์„ ๋บ€ ๋‚˜๋จธ์ง€๋ฅผ ๋„ฃ์Œ
์ฃผ์„๊ณผ ์•„๋ž˜์˜ ์ฝ”๋“œ๊ฐ€ ๋™์ผํ•˜๋ฉฐ slice()์™€ splice()์˜ ์ฐจ์ด๋Š” ์•„๋ž˜์— ์ ์–ด๋‘์—ˆ์Œ

 

์ฐพ์•„๋ณธ ๋‚ด์šฉ, ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ๋“ค

findIndex() - ์ผ์น˜ํ•˜๋Š” ๊ฐ’์˜ ์ƒ‰์ธ์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉ