ECMA Script 6 - Object의 for문

박성원·2020년 11월 19일
0

ECMA6

목록 보기
8/10
post-thumbnail

for ~ of

반드시 iterable 객체만 가능하고 실제값 반환한다.

  <script type="text/javascript">
    //for~of
    let a = [10, 20, 30];
    for (let x of a) {
      // 방 하나에 있는 값의 실제 값을 뽑아온다.
      console.log('of>>>', x);
    }
let b = 'hello';
for (let x of b) {
  console.log('of>>>', x);
}
``` ## for ~ in ### index 값에 접근 ``` //for~ in index값에 접근 let c = [10, 20, 30]; for (let x in c) { // index값에 접근한다. console.log('in>>>', x); } let d = 'hello'; for (let x in d) { console.log('in>>>', x); } ``` ## 객체 (json)은 iterable 객체가 아니기 때문에 for~ of 사용 불가하다. ``` // object let e = { one: 100, two: 200 }; // for (let x of e) { // console.log('of>>>', x); //error // } let f = { one: 100, two: 200 }; for (let x in f) { console.log('in>>', f[x]); // 100,200 console.log('in>>>', x); // key값 출력 one,two~~ } ```
profile
개발 일기장

0개의 댓글