简体   繁体   English

如何避免做同样事情的if-else语句?

[英]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. 我试图最小化一个小型js程序,但遇到了这个问题:我不想写很多if-else语句,它们都做同样的事情。 Is there an elegant way to rewrite this function? 有没有一种优雅的方法可以重写此功能?

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;
}

forEach doesn't return anything - use map : forEach不返回任何内容-使用map

arr = arr.map(element => {
  if (element.value) {
    let [wf] = element.value['wait_for'] || [0];
    let [req] = element.value['request'] || [0];
    if (wf && req && [wf, req].includes(toCompare)) element = update_element(element);
  }
  return element;
});

Ignoring for a moment the fact that element = update_element(element); 暂时忽略element = update_element(element);的事实element = update_element(element); achieves nothing (as you're just updating the local element , not what it references) your condition can be reduced to simply 什么都不做(因为您只是更新local element ,而不是它引用的内容),您的条件可以简化为

if((wf && wf[0] == toCompare) || (req && toCompare == req[0]))

Since the basic check is whether toCompare equals either wf[0] or req[0] and you basically add a check to see if wf and req exist. 由于基本检查是toCompare是否等于wf[0]req[0] ,因此您基本上添加了检查以查看wfreq存在。

At this point you can use map instead of forEach to get the result you want. 此时,您可以使用map而不是forEach获得所需的结果。 Notice you have to return the map, and whatever function calls advance_bthreads needs to reassign the result (ie arr = advance_bthreads(toCompare, arr); ) 注意,您必须return映射,无论调用什么函数advance_bthreads需要重新分配结果(即arr = advance_bthreads(toCompare, arr);

function advance_bthrads(toCompare, arr) {
    return arr.map(function(element) { // NOTICE you have to return the result
        if (element.value) {
            let wf = element.value['wait_for'];
            let req = element.value['request'];
            if((wf && wf[0] == toCompare) || (req && toCompare == req[0]))
                element = update_element(element);
        }
        return element;
    }
}

You could short it a bit by returning early and combining the conditions. 您可以通过提前返回并合并条件来缩短它的时间。

BTW, the assignment does not work as intended, you need to use the element of the array with an index. 顺便说一句,分配无法按预期工作,您需要将数组的元素与索引一起使用。

If update_element mutates element , then no assignment is necessary. 如果update_element改变了element ,则不需要赋值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM