I'm interested in this scenario, involving an angular filter.

我对这个情况很感兴趣,包括角滤波器。

I have a column returned from my database which is a boolean, I want to return from my angular filter based on that value. Simple:

我有一个从数据库返回的列,它是一个布尔值,我想根据这个值从我的角过滤器返回。简单:

function importExportFilter() {
    return function (value) {
        var conversion = {
            "true": "Import",
            "false": "Export"
        };

        return allowable[value];
    }
}

That is nice and simple, however it doesn't deal with cases where the value isn't a key in our conversion dictionary.

这很好很简单,但是它不处理在我们的转换字典中值不是键的情况。

Two ways to deal with it:

有两种处理方法:

if (conversion.hasOwnProperty(value)) {
    return conversion[value];
}

return "Unknown";

OR:

或者:

return conversion[value] === undefined ? "Unknown" : conversion[value];

Is there a way to modify the javascript object to basically allow this:

是否有一种方法可以修改javascript对象,使其基本允许:

var conversion = {
    "true": "Import",
    "false": "Export",
    "*": "Unknown"
};

where "*" is just a wildcard. It's not an exceptional circumstance where there is an unknown value so Unknown can be used whenever there is no actual value.

其中“*”只是一个通配符。在没有实际值的情况下,未知的值可以被使用,这不是一个特殊的情况。

I can't seem to think of any other interesting ways to do this. Obviously in a business environment I would always go with the first way to deal with unexpected circumstances.

我似乎想不出其他有趣的方法来做这件事。显然,在商业环境中,我总是采用第一种方法来处理意外情况。

However, I was wondering from a creative aspect, if there were any other interesting solutions to this problem?

然而,我想从创造性的角度来看,这个问题是否还有其他有趣的解决办法?

2 个解决方案

#1


3

You can use a logical or ||.

您可以使用逻辑或||。

It takes the first value if the value is truthy, otherwise the second value.

如果值是真实的,则取第一个值,否则取第二个值。

return conversion[value] || 'Unknown';

or another technique:

或者另一种方法:

var conversion = {
    true: "Import",
    false: "Export",
    unknown: "Unknown"
};

return conversion[x] || conversion.unknown;

更多相关文章

  1. 使用“使用中值排序基数法”实现树状结构(二)
  2. HTML CSS Javascript中 id重复时会发生的 情况
  3. 在不知道序列长度的情况下,在Python中展开多个赋值
  4. 在Linux中查找线程的CPU使用情况
  5. linux查看磁盘使用情况命令
  6. jmap使用以及 linux下查看进程的内存使用情况
  7. 解决useradd 用户后没有添加用户Home目录的情况,Linux改变文件或
  8. 如何在不改SQL的情况下优化数据库
  9. 个人网站对xss跨站脚本攻击(重点是富文本编辑器情况)和sql注入攻击

随机推荐

  1. android学到啥时候就高级了啊
  2. Android表达式计算器
  3. Android Canvas练习(1)画一张报表来玩
  4. Android(安卓)中 MessageQueue 的 native
  5. Android中的Ajax
  6. Android and Iphone (a)
  7. Android省市区三级联动滚轮选择(真实项目
  8. 实用技术经验分享:《Android账户同步备份
  9. Android学习札记52:广播(Broadcast)基础篇
  10. android登录模块之静态登录