gpt4 book ai didi

javascript - 如何使用变量作为字段键获取 Meteor 集合字段值

转载 作者:可可西里 更新时间:2023-11-01 09:59:05 25 4
gpt4 key购买 nike

我将给出一个代码示例来描述我的问题:

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?

最佳答案

尝试使用 bracket notation 访问对象属性:

Collection.findOne(id)[someVariable];

关于javascript - 如何使用变量作为字段键获取 Meteor 集合字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29559391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com