# 객체와 반복문 for\~in

{% hint style="info" %}
객체의 반복을 위한 **for\~in문** 가져오는 법 :&#x20;

search : javascript object iterate
{% endhint %}

### 예시)

![](https://3193435092-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Miu1tqKdNPM-BV0usUx%2F-Mj99MiuA1WR1URL0Q8_%2F-Mj99cJafeDfs7W2fPis%2F%EC%9D%B4%EB%AF%B8%EC%A7%80%20056.jpg?alt=media\&token=a414337c-3df9-4e9c-88a4-ba9f8bb0ef5c)

{% tabs %}
{% tab title="script" %}

```javascript
for(var key in coworkers)
    {document.write(key + ' : ' + coworkers[key] + '<br>');
}
```

{% endtab %}

{% tab title="전체코드" %}

```javascript
<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>
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**key :** 객체에 소속된 변수이자 **프로퍼티(property)**   ex) programmer

**coworkers\[key]** : **프로퍼티의** 정보이자 **배열** (설명이 정확한지 확실치X)   ex) egoing
{% endhint %}

{% hint style="info" %}
**배열**은 key가 아니라 **index**
{% endhint %}
