lundi 20 mai 2019

How to avoid if-else statements that do the same thing?

I'm trying to minimize a little js program, and I came across this problem: I do not want to write a lot of if-else statements that they all do the same thing. Is there an elegant way to rewrite this function in an elegant way?

function advance_bthrads(toCompare, arr) {
    arr.forEach(function(element) {
        if (element.value) {
            let wf = element.value['wait_for'];
            let req = element.value['request'];
            if (wf && req) {
                if (toCompare === wf[0] || toCompare === req[0]) {
                    element = update_element(element);
                }
            } else if (wf) {
                if (toCompare === wf[0]) {
                    element = update_element(element);
                }
            } else if (req) {
                if (toCompare === req[0]) {
                    element = update_element(element);
                }
            }
        }
    });
    return arr;
}

Aucun commentaire:

Enregistrer un commentaire