본문 바로가기

Javascript

Collection 개념

728x90

1. Javascript ES6에서 나온 Collection 개념 및 예시 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Set 개념</title>
  </head>
  <body>

  </body>
  <script>
    /* [Jasvascript ES6] 
       - Set개념이 나옴 
       - 개념: 중복된 값을 허용하지 않는다!      
    */ 
    
    const empty = new Set();
    console.log(empty); //1번, Set(0) {}
    
    const nums = new Set([1, 2, 3]);
    console.log(nums); //2번, Set(3) { 1, 2, 3 }
    
    const duplication_str = new Set('WOW');
    console.log(duplication_str); //3번, 결과값이 new Set(["W","O"]) = {'W', 'O'} 그 이유는 중복 값을 허용하지 않는다! 
    
    const Corporations = new Set([
      {name:'SM1',company:'Samsung Electronics Co., Ltd.'},
      {name:'SM2',company:'Samsung SDI Co., Ltd.'},
      {name:'SM3',company:'RAINBOW ROBOTICS Co., Ltd.'},
      {name:'SM4',company:'Hanhwa System Co., Ltd.'}, 
    ]);
    console.log(Corporations);  //4번 
    //add메서드
    const numsAdd_result = nums.add(4).add(5);
    console.log(numsAdd_result); //5번
  </script>  
</html>

2. 콘솔 결과

console창(F12)

 

728x90

'Javascript' 카테고리의 다른 글

javascript json.parse()  (1) 2023.12.06