خرید بک لینک

Vote count: 0

I'm trying to use Jade for a simple web app for html templating. I have JSON that looks like:

{
    "responses": [
        {
          "fieldOne": 1,
          "fieldTwo": 2,
          "fieldThree": "Some string"
        },
        {
          "fieldOne": 10,
          "field2": 20,
          "fieldFour": "Some other string"
        }
    ]
}

I'm trying to loop over this responses list in Jade, and create a row in a table for each response. The looping seems to work fine, the issue is that some responses are missing fields, and I'd like to put a blank in that cell if the field is missing. I'm passing into Jade a map (from Spark) with only one entry, "responseData", which maps to the whole JSON object.

body
    div
        table
            thead
                tr
                    th Field One
                    th Field Two
                    th Field Three
                    th Field Four
            tbody
                each val in responseData.responses
                    tr
                        td
                            #{val.fieldOne}
                        td
                            #{val.fieldTwo}
                        td
                            #{val.fieldThree}
                        td
                            #{val.fieldFour}

This works right now if each response has all four fields. I'd like to replace these with if conditions checking that the field exists before it tries to read the value. Something like:

td
    if val.fieldFour
        #{val.fieldFour}

This returns 'inaccessible or unknown property fieldFour' if fieldFour doesn't exist. I'd like to know how to check if val has a specific property. I've tried following Jade tutorials, trying things like prepending things with locals, using bracket notation, etc. but to no avail. I haven't found an example where this is done inside a loop, so that might be an issue.

I've looked at all the other stack overflow questions I can, so any help is appreciated! Thanks!

asked 24 secs ago

برچسب: check json property exists,check json property,check if json property exists,check json has property,jquery check json property exists,javascript check json property,check if json property exists php,check if json property is empty, نویسنده: استخدام کار تاريخ: سه شنبه 26 مرداد 1395 ساعت: 5:15

صفحه بندی