개발/JavaScript 30

7. Array Cardio Day 2

Sangminnn 2017. 4. 11. 00:36
반응형

근황

어제 6일차 내용을 거의 다 정리 해놓았는데
갑자기 노트북이 멈췄다. 그래서 부팅해봤는데 ssd가 망가져서
os부터 다시 설치했다. 후
다음주에 ssd를 장착할 때 까지는 조금 느리게 올릴것 같다.
그래서 먼저 7일차 올리고
6일차는 차차 올리도록 하겠다.

코드 분석

<!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 }
    ];

  </script>
</body>
</html>

목표

.some과 .every , 그리고 배열에서 원하는 내용을 가진 자료를 찾아내는 함수를 알아보자.

분석 개요

  1. .some을 사용해보자
  2. .every를 사용해보자
  3. 원하는 것을 배열에서 찾아내보자

분석

const isAdult = people.some(person => ((new Date()).getFullYear()) - person.year >= 19);

먼저, .some 은 해당하는 내용이 일부 참이라면 true를 리턴하고 전부 아니라면 false를 리턴하는 함수이다.

const allAdults = people.every(person => ((new Date()).getFullYear()) - person.year >= 19);

다음은, .every이다.
이것은 해당하는 내용이 전부 참일경우에만 true를 리턴하고 그 이외에는 false를 리턴한다.

const comment = comments.find(comment => comment.id === 823423);
const index = comments.findIndex(comment => comment.id === 823423);

위에 있는 .find부터 보자
이것은 원하는 내용과 일치하는 배열 내의 인자를 찾아내는 함수이다.
그리고 아래의 .findIndex 는 말 그대로 위에 있는 내용과 index라는 것을 합친것이다.
내용과 일치하는 배열 내의 인자의 배열 내의 번호를 알려준다.


위의 코드대로 했을 때 콘솔창에 나오는 결과이다.

기본적인 정리 틀은 이정도이다.

끝.

반응형