[30일 챌린지 Day-7] some,every,find,findIndex

2022. 7. 26. 17:13·개발/html, css, js
728x90
반응형

콘솔로 비교해보기

 

1. 기본 값

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

    // Some and Every Checks
    // Array.prototype.some() // 19살 보다 많은 사람이 있는지?
    // Array.prototype.every() // 모두가 19살보다 많은지?

    // Array.prototype.find()
    // ID가 823423 인것을 찾으시오

    // Array.prototype.findIndex()
    // ID가 823423인 것의 인덱스를 표시
    // comment에서 ID 가 823423 인것을 제거

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

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

      console.log(
        people.some((el) => {
          let currentYear = new Date().getFullYear();
          return currentYear - el.year > 19;
        })
      );

      console.log(
        people.every((el) => {
          let currentYear = new Date().getFullYear();
          return currentYear - el.year > 19;
        })
      );

      console.log(
        comments.find((el) => {
          return el.id == 823423;
        })
      );

      let index = comments.findIndex((el) => {
        return el.id == 823423;
      });
      console.log(index);

      // comments.splice(index,1);
      // console.log(comments);

      //또는

      const newComments = [
        ...comments.slice(0, index),
        ...comments.slice(index + 1),
      ];

      console.log(newComments);
    </script>
  </body>
</html>

3. 리뷰

각 인터페이스 함수의 사용법을 숙지.

배열안에 ...으로 각요소추출, 없으면 무시하여 각요소를 새로운 배열에 담아줌

728x90
반응형

'개발 > html, css, js' 카테고리의 다른 글

[JS] 쿠키와 세션  (0) 2022.08.09
[JS] 쿠키와 세션  (0) 2022.08.08
[30일 챌린지 Day-6] 검색, json 활용  (0) 2022.07.26
[30일 챌린지 Day-5] flex, classname 추가 활용  (0) 2022.07.25
[30일 챌린지 Day-4] 배열,객체 알고리즘  (0) 2022.07.25
'개발/html, css, js' 카테고리의 다른 글
  • [JS] 쿠키와 세션
  • [JS] 쿠키와 세션
  • [30일 챌린지 Day-6] 검색, json 활용
  • [30일 챌린지 Day-5] flex, classname 추가 활용
TeTedo.
TeTedo.
  • TeTedo.
    TeTedo 개발 일기
    TeTedo.
  • 전체
    오늘
    어제
    • 분류 전체보기 (319)
      • 개발 (274)
        • Article (4)
        • 정리 (21)
        • Spring Boot (17)
        • JPA (2)
        • JAVA (6)
        • Database (4)
        • 자료구조 (11)
        • 알고리즘 (32)
        • React (20)
        • Docker (10)
        • node.js (18)
        • Devops (11)
        • Linux (4)
        • TypeScript (3)
        • Go (10)
        • HyperLedger (4)
        • BlockChain (43)
        • html, css, js (48)
        • CS (3)
        • AWS (3)
      • 모아두고 나중에 쓰기 (3)
      • 팀프로젝트 (18)
        • SNS(키보드워리어) (9)
        • close_sea (9)
      • 개인프로젝트 (1)
        • Around Flavor (1)
        • CHAM (13)
        • ethFruitShop (5)
      • 독서 (0)
        • 스프링부트와 AWS로 혼자 구현하는 웹 서비스 (0)
  • 블로그 메뉴

    • 홈
    • 개발일기
    • CS
    • 실습
    • 코딩테스트
    • 웹
    • Go
    • node.js
    • 팀플
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    하이퍼레저
    프로그래머스
    mysql
    html
    go언어
    node
    js
    ERC721
    컨테이너
    erc20
    30일챌린지
    React
    nodejs
    go
    블록체인
    명령어
    30일 챌린지
    CSS
    node.js
    도커
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
TeTedo.
[30일 챌린지 Day-7] some,every,find,findIndex
상단으로

티스토리툴바