有没有办法用&&侦测null值 不用这么多个if

如题
假设我有一个function 我要取一个东西,但我不清楚它的type是什么我要去各个map里面去查找
就需要先侦测map里面是否有null值,然后做回传

就会变成我每次都要判定map是否null 如下

get(name: string) {
        if (this.type1Map.get(name) != null) {
            return this.type1Map.get(name);
        }
        if (this.type2Map.get(name) != null) {
            return this.type2Map.get(name);
        }
        if (this.type3Map.get(name) != null) {
            return this.type3Map.get(name);
        }
    }

其实我常常会需要一个return值,但我不知道它是否是null的状况
我很想用return (this.type1Map.get(name)!=null)? this.type1Map.get(name) : 不回传;
类似这样短短一行,但同时判定如果null就不回传,不知道各位大神有没有更好的写法?

1赞

可否

return this.type1Map.get(name) || this.type2Map.get(name) || this.type3Map.get(name);

该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。