使用数组中的键的车把查找对象? [英] Handlebars lookup object using key from array?

查看:59
本文介绍了使用数组中的键的车把查找对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数组和对象的对象.我对数组(字段)进行迭代-将其用作创建表单元素的模板.我要-在此过程中-从对象(数据)中获取相应的对象值.

I have an object with an array and an object. I iterate the array (fields) - using it as a template to create form elements. I want to - in the process - get the corresponding object value from the object (data).

{
    "fields": [
        {
            "name": "id",
            "type": "int",
            "max_length": 11,
        },
        {
            "name": "email",
            "type": "varchar",
            "max_length": 191,
        }
    ],
    "data": {
        "id": "4",
        "email": "person@domain.com",
    }
}

类似的东西(思维导图):

Something like this (mind map):

{{#each fields}}
    <li>
        lookup {{lookup ../data => VALUE OF CORRESPONDING KEY ("id" or "email" etc.)}}
        <label for="{{name}}">{{CamelCase name}}</label>
        {{InputType name type}}
    </li>
{{/each}}

因此,当字段名称为'id'时,我想从对象等中获取'id'的值.

So, when the field name is 'id' I'd like to grab the value of 'id' from the object etc.

我似乎无法全神贯注于查找...或者还有其他更聪明,更直接的方法来实现此目的吗?

I can't seem to wrap my head around the lookup ... or are there some other more clever and direct way of achieving this?

推荐答案

谢谢,最终做到了这一点(将数据传递给助手):

Thanks, ended up doing this (passing the data to the helper):

{{#each fields}}
    <li>
        <label for="{{name}}">{{CamelCase name}}</label>
        {{InputType name type ../data }}
    </li>
{{/each}}

然后在助手中通过以下操作提取值:

And then in the helper extracted the value by doing:

Handlebars.registerHelper("InputType", function ( name, type, value) {
    switch ( type ) { …
    … value="' + value[name] + '">');

这篇关于使用数组中的键的车把查找对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆