Handelbars查找helper返回对象并将键值分配给模板变量



我添加了一个通用帮助器来查找数组中的项。

module.exports = function(array, findFunctionString) {
const fn = new Function("return" + findFunctionString)();
return array.find(fn)
}; 

我的数组就像:

[{label: "foo", selected: true}, {label: "bar", selected: false}]

我现在正在寻找的是获得结果并分配给具有此返回对象的特定键的模板变量。

{{#> myTemplate myVar=(find myArray "(el) => el.selected").label}}{{/myTemplate}}

我仍然得到一个错误

Expecting 'CLOSE_RAW_BLOCK', 'CLOSE', 'CLOSE_UNESCAPED', 'OPEN_SEXPR', 'CLOSE_SEXPR', 'ID', 'OPEN_BLOCK_PARAMS', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'SEP'

然而,如果删除".label",没有错误,对象被很好地分配给myVar。但是我只想赋值键值label

为什么我不能访问返回对象的键?

解决方案是使用附加的lookup助手。

label=(lookup (find data.languageSelector.options "(el => el.selected)") "label")

最新更新