tech.junha.kr

Iterable과 Enumerable, 한 번에 정리하기

한 방 정리

뜻함수뜻타입예시
Iterable반복 가능한for (value of arr)순회하면서 값에 접근할 수 있는가iterator 객체가 있는 데이터 타입Array, map, set,
nodeList, String
Enumerable열거 가능한for ( key in obj)객체의 key에 접근할 수 있는가객체 타입Object

예제로 살펴보자

// 배열 예시
let arr = ['Junha', 'Rany', 'Leo']
arr.hobit = 'Prodo'

// 객체 예시
let obj = {
	firstKey: ['Nunu','Garen'],
    secondKey:['Yumi','Galio','Ryze'],
    thirdKey:['Minsu','heejung']
}

obj.fourthKey = 'Junha'

1. for in 은 key에 접근한다.

for (let key in arr) {
	console.log(key)
}

// 0
// 1
// 2
// hobit
for (let key in obj) {
	console.log(key)
}

// firstKey
// secondKey
// thirdKey
// fourthKey

2. for of 는 value에 접근한다.

  • for of를 돌기 위해선 iterator 객체가 있어야 한다.

  • Iterable 객체는 유사배열객체에 존재한다.

for (let value of arr) {
	console.log(value)
}

// Junha
// Rany
// Leo
// Prodo (출력x)

hobit 프로퍼티는 key가 012.. 순으로 들어가 있는 배열 요소를 갖춘 형태가 아니다.

이는 iterable 하지 않다는 뜻이기에, for of 반복문에서 제외됐다.

for (let value of obj) {
	console.log(value)
}

// TypeError: obj is not iterable

object는 iterator가 없기 때문에 for of 반복문을 돌 수 없다.

iterable하게 설정하려면 cutstom configuration이 필요하다.

for (let value of firstKey.town) {
	console.log(value)
}

// Nunu
// Garen

obj.firstKey는 배열이다.

배열은 iterable하기 때문에 for of 반복문을 돌 수 있다.

for (let value of obj.fourthKey) {
	console.log(value)
}

// J
// u
// n
// h
// a

string도 마찬가지로 iterable하기 때문에, for of 반복문을 돌 수 있다.

메소드 및 프로퍼티

1. enumerable 속성

  • for..in 반복문이나, Object.keys()를 통해 오브젝트 속성을 접근할 수 있는지 정의한다.
  • 기본적으로 모든 속성은 enumerable:true로 초기화된다
const yoonjung = {
	home: 'seoul',
	type: 'isfp'
}

yoonjung.major = 'sprots'

for (const key in yoonjung) {
	console.log(key)
}

// home
// type
// major

2. Object.defineProperty()

특정 필드를 enumerable에 해당되지 않게 설정할 수도 있다.

enumerable:false로 설정하면, Object.keys에 반환이 안되고 for in 반복문을 돌지도 않는다.

Object.defineProperty(obj, 'key', {
  enumerable: false,
  configurable: false,
  writable: false,
  value: 'static'
});
Object.defineProperty(yoonjung, 'height', {
    enumerable: false,
    value: 170
});

for (const key in person) {
    console.log(key);
}

// home
// type
// major
// height는 안나옴

3. object.propertyIsEnumerable

해당 필드가 enumerable한지 확인할 수 있다.

console.log(yoonjung.propertyIsEnumerable('home')); // => true
console.log(yoonjung.propertyIsEnumerable('type')); // => true
console.log(yoonjung.propertyIsEnumerable('major')); // => true
console.log(yoonjung.propertyIsEnumerable('height')); // => false

4. Object.keys(obj) : array

returns all enumerable own properties

var a = {};

Object.defineProperties(a, {
    one: {enumerable: true, value: 1},
    two: {enumerable: false, value: 2},
});

Object.getOwnPropertyNames(a); // ["one", "two"]
Object.keys(a); // ["one"]

5. Object.getOwnPropertyNames(obj) : array

returns all own properties of the object.

enumerable 속성 값에 상관없이, 오브젝트의 모든 프로퍼티를 반환한다.

6. Object.prototype.hasOwnProperty(property) : boolean

객체가 특정 프로퍼티를 가지고 있는지를 나타내는 불리언 값을 반환한다.

o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop');             // returns true
o.hasOwnProperty('toString');         // returns false
o.hasOwnProperty('hasOwnProperty');   // returns false

7. summary

  • A property is enumerable if it has the enumerable attribute sets to true.
  • The obj.propertyIsEnumerable() determines whether or not a property is enumerable.
  • A property created via a simple assignment or a property initializer is enumerable.