Hakki
Hakki

Reputation: 23

How to get Meteor collection field value using variable as field key

I'll give a code example to describe my problem:

var id = Collection.insert({
    name: 'Charles Darwin',
    likes: 1
});

var someVariable = 'name';

Collection.findOne(id).name // This returns 'Charles Darwin', but how do I use someVariable to get the same result?
Collection.findOne(id).someVariable // This will certainly not work, but what is the right way to do it?

Upvotes: 2

Views: 2307

Answers (1)

chridam
chridam

Reputation: 103475

Try accessing the object property using the bracket notation:

Collection.findOne(id)[someVariable];

Upvotes: 5

Related Questions