【问题标题】:How to get array object in object如何在对象中获取数组对象
【发布时间】:2015-07-29 02:34:17
【问题描述】:

以下是我收藏的示例文档:

{
    "_id" : "0001",
    "name" : "Meteor",
    "categoryId" : "001",
    "period" : 4,
    "price" : 68,
    "des" : "សម្រាប់ការចុះឈ្មោះ៥០នាក់ដំបូង",
    "paymentMethod" : {
        "oneMonth" : 100,
        "threeMonth" : 150,
        "sixMonth" : 375,
        "year" : 750,
        "midTerm" : 200,
        "term" : 750
    }
}

如何从"paymentMethod" 键中获取值以显示在选择框中?

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:
    <select>
        {{#each methods}}
            <option value={{value}}>{{method}}</option>
        {{/each}}
    </select>
    
    Template.body.helpers({
        methods: function () {
            var ArrMethods = [];
            var objPayment = MyCollection.findOne({_id: '0001'});
            $.each(objPayment.paymentMethod, function (key, value) {
                ArrMethods.push({
                    method: key,
                    value : value
                });
            });
            return ArrMethods;
        }
    });
    

    【讨论】:

    • @SylvainLeroux 问题很简单,所以答案是。另一件事是我提供的代码是可读的(代码会说话)。问题是在how(代码)而不是why(如果提问者或回答者要求)的上下文中。
    【解决方案2】:
    Template.name.helpers({
        'selectOptions': function () {
            var obj = MyCollection.findOne({_id: '0001'});
            var options = obj && obj.paymentMethod;
            var helperReadyOptions = [];
            Object.keys(options).forEach(function(key) {
                helperReadyOptions.push({
                    label: key,
                    value: options[key]
                });
            });
            return helperReadyOptions;
        }
    });
    
    <template name="name">
        <select>
            {{#each selectOptions}}
                <option value="{{value}}">{{label}}</option>
            {{/each}}
        </select>
    </template>
    

    【讨论】:

    • 也许您应该考虑在您的代码中添加一些 cmets 和/或在您的回答中添加一些关于为什么您这样做的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    相关资源
    最近更新 更多