Javascript

Map() - 반복문

자라나라나무나무나 2022. 5. 28. 19:14
Collection 객체인 Map이 가지고 있는 iterator 속성을 이용하여 for ... of 구문을 통해 반복문 수행 가능
let recipe_juice = new Map([
    ["banana", 50],
    ["melon", 60],
    ["lemon", 70],
]);

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

for(let amount of recipe_juice.values()) {
    console.log(amount);
};

// 전체(key, value)
for(let entity of recipe_juice) {
    console.log(entity);
};

// 전체(key, value) - 결과동일
for(let entity of recipe_juice.entries()) {
    console.log(entity);
}