0

This is my code (not working)

return monitor if (monitor.uuid is $scope.selectedMonitor) for monitor in $scope.monitors

I want to return monitor if the if is true and I'm trying to do in one single line. Is it possibile?

1 Answer 1

2

You could use the when keyword:

getSelectedMonitor = ->
  return monitor for monitor in $scope.monitors when monitor.uuid is $scope.selectedMonitor

This generates the following 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;
    }
  }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks. Do you know if it is possible to append an "else" statement? Something like this: when condition else null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.