简体   繁体   中英

Check if value of keys is a string with Handlebars

I have an object like this:

{
  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
}

Is there any way that I can only get the keys where the values are a string using handlebars helpers?

You can write handlebars configuration helper which can check this

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)
    }
})

Now When writing your.hbs code you can include

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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