프론트엔드 센트럴파크 (☞゚ヮ゚)☞

set() - 반복문 본문

Javascript

set() - 반복문

자라나라나무나무나 2022. 5. 31. 01:17

Set.keys()

Set.values()

let str = new Set("Hello");

console.log(str);

for(let item of str) { // value가 item으로 리턴
    console.log(item);
}

key가 없기 때문에 value가 리턴되는 것을 볼 수 있다.

 


let str = new Set("Hello");

console.log(str);

for(let item of str) {
    console.log(item);
}
for(let item of str.keys()) {
    console.log(item);
}
for(let item of str.values()) {
    console.log(item);
}

key 값이 없기 때문에 세개의 반복문 다 value 값만 출력되는 것을 볼 수 있다.

 


Set.entries()

let str = new Set("Hello");

console.log(str);

for(let item of str.entries()) {
    console.log(item);

key, value 형태로 포맷을 지켜서 반환을 한다.

그 이유는 Map과의 호환성을 지키기 위해서 이다. 

key 와 value값 둘 다 같은 값이라고 생각하면 된다.

Comments