繁体   English   中英

检查键的值是否是带有 Handlebars 的字符串

[英]Check if value of keys is a string with Handlebars

我有一个像这样的 object:

{
  id: 1,
  type1: 'Adjective',
  type2: 'Noun',
  type3: 'Noun',
  type4: 'Noun',
  type5: 'Plural Noun',
  type6: 'Person',
  type7: 'Place',
  type8: 'Job',
  type9: 'Adjective',
  type10: 'Adjective',
  type11: 'Famous Person',
  type12: 'Noun',
  type13: 'Noun',
  type14: '',
  createdAt: 2019-10-11T06:49:38.000Z,
  updatedAt: 2019-10-11T06:49:38.000Z
}

有什么方法可以让我只能使用车把助手获取值是字符串的键?

你可以编写车把配置助手来检查这个

module.exports.hbs = exphbs.create({
    defaultLayout: 'layout',
    helpers: {
        //You can Name this anything
        checkIfValuesOfJsonAreStrings: function(jsonList, context) {
        for (var key in obj) {
         if(typeOf obj[key] == "String"){

        }else{
           return context.inverse(this); // This False
        }
      }
      return context.fn(this)
    }
})

现在在编写 your.hbs 代码时,您可以包括

{{# checkIfValuesOfJsonAreStrings}}
   <div>Yah all the values in JSON are string</div>
{{/checkIfValuesOfJsonAreStrings}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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