在javascript中我有以下内容:
var inf = id + '|' + city ;
Run Code Online (Sandbox Code Playgroud)
如果id或city为null,则inf为null.
是否有任何光滑的方式说如果id或city为null则为空.
我知道在c#中你可以做到以下几点:
var inf = (id ?? "") + (city ?? "");
Run Code Online (Sandbox Code Playgroud)
在javascript中有任何类似的方法吗?
psy*_*yho 20
怎么样:
var inf = [id, city].join('|');
Run Code Online (Sandbox Code Playgroud)
编辑:你可以在加入之前删除"空白"部分,这样如果id和city中只有一个为null,则inf将只包含该部分,如果两者都为null,则inf为空.
var inf = _([id, city]).compact().join('|'); // underscore.js
var inf = [id, city].compact().join('|'); // sugar.js
var inf = [id, city].filter(function(str) { return str; }).join('|'); // without helpers
Run Code Online (Sandbox Code Playgroud)
Ell*_*lle 13
总远投,但试试这个:
var inf = (id || "") + "|" + (city || "");
Run Code Online (Sandbox Code Playgroud)
var inf = (id == null ? '' : id) + '|' + (city == null ? '' : city)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21106 次 |
| 最近记录: |