Using a variable name as a column name in TaffyDB

986 views Asked by At

I am a novice to taffydb, faced following issue when trying to querying the db. My db has a column named TYPE. I tried to get all record where TYPE equals to "Yes".

This line give me the correct results:

var records = database({ "TYPE": { "===": "Yes" } });

But if I tried to pass these values as parameters no records found.

eg.

var column= "TYPE";
var operator = "===";
var value = "Yes" ;
var records = database({ column: { operator: value } });

Simply I need to pass these values as method parameters not in the hard-coded way. What should be the correct way to get this done?

1

There are 1 answers

0
Darksbane On

You'll need to treat the object like an array in order to get it to look at the appropriate data

http://jsfiddle.net/Darksbane/kjCtY/

var products = TAFFY([{
  "item":1,
    "name":"Blue Ray Player",
    "price":99.99,
    "type":"No"
}, {
    "item":2,
    name:"3D TV",
    price:1799.99,
    "type":"Yes"
}]);

var column= "type";
var operator = "===";
var value = "Yes" ;
var object = {};
object[column]={};
object[column][operator]=value;
console.log(object);
var records = products(object).get();
console.log(records);
$('#myul').append('<li>'+records[0].name+'</li>');