user95227 user95227 - 3 months ago 11
Javascript Question

How to write a taffyDB query using a variable as an attribute title?

I'm using taffyDB to query my javascript objects. I have a database

db()
, where each item in the db has an attribute
Test Field
and an attribute
Test_Field
.

If I want to query all the items in
db()
for which the value of
Test_Field
is "test", the following works:

var dbQuery = db({ Test_Field:"test" }).get()


However, if I want to query all the items in
db()
for which the value of
Test Field
is "test", I cannot find any query that works. The following are my best attempts.

var dbQuery = db({ Test Field:"test" }).get()

var fieldName = "Test Field"
var dbQuery = db({ fieldName:"test" }).get()

var field = {}
field[name] = "Test Field"
var dbQuery = db({ field[name]:"test" }).get()


I want to grab the items where
Test Field
is
"test"
Any ideas how to query with a variable name so that I can check an attribute with space in its name like
Test Field
?

Thank you very much for your time. Let me know if I am being unclear or if you need anything else from me.

Answer

Have you tried db().filter({"Test Field": "test"}); or var obj = {}; obj[fieldName] = "test"; db().filter(obj);