Search code examples
javascriptnode.jsloopback

Why doesn't parameter value get replaced?


I have this snippet of code

function(memo, value, key) {
    if (!!value) {
      memo.push({
        'property': 'loopbackFilter',
        'value': {
          key : {
            'like' : '%' + value + '%'
          }
        }
      });
    }
    return memo;
  }

Sublime Text says that "key" parameter is not being used, and in fact the resulting JSON object is

"where": {
  "key": { <--- key is literally "key"
    "like": "%value%" (VALUE is an actual value, so it works here)
  }
}

Why does this happen?


Solution

  • JS object literals are different than JSON objects: In JavaScript you don't need to quote the property names:

    var obj = { foo: "bar" }
    

    For computed property names use the bracket syntax:

    [key] : {
      'like' : '%' + value + '%'
    }
    

    Please also note that this is an ES6 feature.