728x90
반응형
<!DOCTYPE html>
<html lang="ko">
    <head>
        <title>오브젝트</title>
            <meta charset="UTF-8">
            <script>
                // 오브젝트 : 키-값 쌍으로 이루어진 것(HashMap 이랑 비슷)
                // 키는 항상 문자열! 숫자 불가, 쌍/홑따옴표 생략 가능  
                // 키와 값은(:)으로 구분한다.
                let students = {
                    김학생:'kim',
                    이학생:'Lee',
                    박학생:'Park'
                };
                console.log(students['김학생']); //kim
                console.log(students.이학생); //Lee
               
                //쌍 추가
                students.최학생 = 'Choi';
                //students['최학생'] = 'Choi';
               
                //쌍 수정
                students.박학생 = 'Bak';
               
                // 쌍 삭제
                delete students.최학생;

                // 키 배열
                let keys = Object.keys(students); //['김학생', '이학생', '박학생']
                console.log(keys);
                console.log('------');

                keys.forEach(key => {
                    console.log(students[key]);
                });

               
               
               
                Object.keys(students).forEach(key => {
                    console.log(students[key]);
                })


                //값 배열
                Object.values(students); //[ 'Kim', 'Lee', 'Bak']

            </script>
    </head>
    <body>

    </body>
</html>
728x90
반응형

'코딩일지 > WEB' 카테고리의 다른 글

이벤트(addEventListener)  (0) 2022.12.25
요소선택  (2) 2022.12.25
연산자  (0) 2022.12.25
변수  (0) 2022.12.25
배열  (0) 2022.12.25

+ Recent posts

">