简体   繁体   中英

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?

尝试使用方括号表示法访问object属性:

Collection.findOne(id)[someVariable];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM