我有这个GorillaScript代码来扁平数组:
Array::compact := #
for filter value in this
value and (typeof value.isempty != 'function' or not value.isempty()) and (typeof value != 'object' or Object.keys(value).length != 0)GorillaScript有点死了。能帮我把这个翻译成LiveScript吗?我对LiveScript很陌生。
发布于 2016-04-03 19:26:55
直译应该是这样的。
Array.prototype.compact = ->
[v for v in @ when v and (typeof v.isempty isnt \function or not v.isempty!) and (typeof v isnt \object or Object.keys value .length > 0)]一个更惯用的例子可能是:
is-empty = ->
| not it => false
| typeof it.isempty isnt \function or not it.isempty! => true
| typeof it isnt \object or not Object.keys it .length > 0 => true
| otherwise => it
Array.prototype.compact = -> [x for x in @ when not is-empty x]注意,这是在我头上做的--我的LiveScript有点生疏了,但是这里的一般想法是可以的。
https://stackoverflow.com/questions/36389743
复制相似问题