ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 8.Object2
    JavaScript 2021. 10. 1. 16:01
    반응형
    const user = {
        id: 1,
        name: "kim",
        job: "programmer"
    }
    
    user.id; //1
    
    const key = "id"; //속성 "id"에 대한 이름 key 선언
    
    user[key]; //1
    
    //응용
    
    const getValue = (user, keyToRead) => {
        return user[keyToRead];
    }
    
    getValue({id: 2, name: "Lee"}, "name"); //Lee. => 객체의 name속성 불러오기
    getValue({id: 2, name: "Lee"}, "id"); //2. => 객체의 name속성 불러오기
    
    //read keys, values
    
    const keys = Object.keys(user); //[ 'id', 'name', 'job' ] : array형식으로 반환
    const values = Object.values(user); //[ 1, 'kim', 'programmer' ]
    
    //동적으로 다루기 : forEach
    
    keys.forEach(key => {
        console.log(key); //객체의 key들을 하나씩 출력
    })
    
    
    //객체 모든요소 펼치기 : entries()
    
    const entries = Object.entries(user); //[ [ 'id', 1 ], [ 'name', 'kim' ], [ 'job', 'programmer' ] ]

     

    반응형

    'JavaScript' 카테고리의 다른 글

    10. chainning, Nullish Coalescing  (0) 2021.10.03
    9.Object3  (0) 2021.10.03
    7.Array 2  (0) 2021.10.01
    6.Objects  (0) 2021.09.27
    5.Arrays  (0) 2021.09.27

    댓글

Designed by Tistory.