Q:

What is the best way for converting if statements into functions?

I have a function that returns object for payload depending on type option (it can be '0', '1' or '2'). What is the best approcach for refactoring multiple if statements into more reusable/functional way?
    const getPayload = type => {
        if (type === '0') {
            return {
                amount: current_amount || amount,
                type: 'TYPE_TEST1',
            }
        }

        if (type === '1') {
            return {
                percent: current_amount || amount,
                type: 'TYPE_TEST2',
            }
        }

        if (type === '2') {
            return {
                percent: current_amount || amount,
                type: 'TYPE_TEST3',
            }
        }
    }

A:

You can map the type with the values using an object, and then just return the value for any type that you get.
var types = {
    0: {
            amount: current_amount || amount,
            type: 'TYPE_TEST1',
    },
    1: {
            percent: current_amount || amount,
            type: 'TYPE_TEST2',
    },
    2: {
            percent: current_amount || amount,
            type: 'TYPE_TEST3',
    }
}

function getType(type) {
    return types[type];
}

