JavaScript

[Javascript] Object활용해서 if 조건문 정리 (refactoring if conditions)

mhui123 2022. 9. 4. 22:04
반응형

아래와 같은 if문이 있습니다.

해당 if문은 이렇게도 작성할 수 있습니다. (if 조건을 담은 객체를 만들어 활용)

const messages = {
    received: "Restaurant started working on your order.",
    prepared: "Driver is picking up your food.",
    en_route: "Driver is driving your way.",
    arrived: "Enjoy your meal."
};

const getPushMessage2 = status => {
    return messages[status] ?? "Unknown status"
}

이를 사용해 object가 해당 key값을 가지고 있는지도 확인하여 분기처리를 할 수도 있다.

 

반응형