这是我的代码(不工作)
return monitor if (monitor.uuid is $scope.selectedMonitor) for monitor in $scope.monitors
如果if为真,我想返回监视器,我想在一行中做。可能吗?
答案 0 :(得分:2)
您可以使用when
关键字:
getSelectedMonitor = ->
return monitor for monitor in $scope.monitors when monitor.uuid is $scope.selectedMonitor
这会生成以下JS:
var getSelectedMonitor = function() {
var monitor, _i, _len, _ref;
_ref = $scope.monitors;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
monitor = _ref[_i];
if (monitor.uuid === $scope.selectedMonitor) {
return monitor;
}
}
};