Coffeescript 函数返回给定值时对象的键

Coffeescript 函数返回给定值时对象的键,coffeescript,Coffeescript,如果给定一个对象,例如: taste = 0: "Hate" 1: "Really Dislike" 2: "Dislike" 3: "" 4: "Like" 5: "Really Like" 6: "Love" 我正在寻找一个函数,如果给定值,它将返回键。我尝试了以下方法 Object::getKeyByVal = (value) -> for prop of this return prop if

如果给定一个对象,例如:

  taste =
    0: "Hate"
    1: "Really Dislike"
    2: "Dislike"
    3: ""
    4: "Like"
    5: "Really Like"
    6: "Love"
我正在寻找一个函数,如果给定值,它将返回键。我尝试了以下方法

  Object::getKeyByVal = (value) ->
    for prop of this
      return prop  if this[prop] is value  if @hasOwnProperty(prop)
使用此函数,alert time.getKeyByVal(<1.5小时)将实际返回8,但它给出的错误将终止脚本的其余部分

Uncaught TypeError: Object function (value) {
      var prop;
      for (prop in this) {
        if (this.hasOwnProperty(prop) ? this[prop] === value : void 0) {
          return prop;
        }
      }
    } has no method 'exec' 
是否有更好的方法获取给定值的键?

尝试以下方法:

getKeyFromValue = (obj,value)->
  for own k,v of obj
    if v==value
      return k

o =
  a:1
  b:2
  c:3
  d:4

console.log(getKeyFromValue(o,3)) # should output c