객체 { }
(Object)
데이터가 추가되면 배열 전체에서 중복되지 않는 인덱스가 자동생성되어 추가된 데이터에 대한 식별자가 되며 이 인덱스를 이용해서 데이터를 가져오게 되는 것인데, 만약 인덱스로 숫자가 아닌 문자를 사용하고 싶다면 객체를 사용해야 함
1. 객체
1-1. 객체의 생성
<script>
var coworkers = {
"programmer":"egoing", //객체
"designer":"leezche"
};
</script>
1-2. 객체의 데이터 불러오기
<script>
var coworkers = {
"programmer":"egoing",
"designer":"leezche"
};
document.write(coworkers.programmer); //egoing
document.write(coworkers.designer); //leezche
</script>
1-3. 객체에 데이터 넣기

coworkers.bookkeeper = "duru"; //데이터추가
document.write(coworkers.bookkeeper); //duru
2. 객체와 반복문
<h1>Object</h1>
<h2>Create</h2>
<script>
var coworkers = {
"programmer":"egoing",
"designer":"leezche"
};
document.write("programmer : " + coworkers.programmer + "<br>");
document.write("designer : " + coworkers.designer + "<br>");
coworkers.bookkeeper = "duru";
document.write("bookkeeper : " + coworkers.bookkeeper + "<br>");
coworkers["data scientist"] = "taeho";
document.write("data scientist : " + coworkers["data scientist"] + "<br>");
</script>
<h2>Iterate</h2>
<script>
for(var key in coworkers){
document.write(key + ' : ' + coworkers[key] + '<br>');
}
</script>
Last updated
Was this helpful?