Struct
Struct

Reputation: 970

replace string with variable

I want to replace the string score20130901 with a variable (flag.currentflag) from mongodb:

var selected_book = Books.findOne({_id: book});  // works
var flag = Flags.findOne({_id: "bookflag"});  // flag.currentflag works
var tmpone = Flags.findOne({_id: "one"}); // works

if (selected_book.score20130901[tmpone.oneround] > 0)

I've tested all variables with alert(...); and I get the correct value from each variable. But If I replace score20130901 with flag.currentflag - it doesn't work.

Error: Uncaught TypeError: Cannot read property '0' of undefined

Upvotes: 0

Views: 73

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

Notation object.property is equivalent to object["property"].

Therefore you can use selected_book[flag.currentflag], which will yield the same as selected_book.score20130901.

Upvotes: 1

Related Questions