[JavaScript30] ๐Ÿ“„ 07. ARRAY CARDIO DAY2

์กฐ์ค€ํ˜•ยท2021๋…„ 7์›” 5์ผ
0

JavaScript30

๋ชฉ๋ก ๋ณด๊ธฐ
7/30

๐Ÿ“„ 07. ARRAY CARDIO DAY2

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฉ”์†Œ๋“œ ์ตํžˆ๊ธฐ.

  • some()
  • every()
  • find()
  • findIndex(), slice()

์ดˆ๊ธฐ์ฝ”๋“œ

<!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>

๐ŸŒ ๋ฌธ์ œ

๐Ÿ‘‰ 1. Some and Every Check

  • 19์„ธ์ด์ƒ์˜ ์‚ฌ๋žŒ์„ ์ถœ๋ ฅ. (some, every์ด์šฉ)

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);

๐Ÿ‘‰ 2. Array.prototype.find()

  • ID๊ฐ€ 823423์ธ comment ์ฐพ๊ธฐ.
const comment = comments.find(function(comment){
        if(comment.id === 823423){
            return true;
        }
    });

ES6 Arrow Function

const comment = comment.find(comment => comment.id === 823423);

๐Ÿ‘‰ 3. Array.prototype.findIndex()

  • findIndex๋กœ id๊ฐ€ 823423์ธ comment์ฐพ์•„์„œ ์ง€์šฐ๊ธฐ.
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๊ฐœ ์‚ญ์ œ

profile
๊นƒํ—ˆ๋ธŒ : github.com/JuneHyung

0๊ฐœ์˜ ๋Œ“๊ธ€