简体   繁体   中英

Can I create dynamic variable names in jade using an array of string values for an object?

I have an array of values named values . I have an each statement that I am iterating through the array. Within the each statement I am using a for loop to iterate through an array of objects. When I iterate through the objects I want to check and see if they have a true boolean variable that shares the name of the items in my values array.

- var values = ["u10","u11","u14","u17","u18"];
  each val in values.length ? values : ['There are no values']
    -for obj in objects
      if obj.val == true
        [do something here]

My if statement is currently not working. Does anyone know how to write this if statement in Jade? Each object has a boolean variable that corelates to the values in the list. A model for the objects looks like so:

{"object":{
   "u10":true,
   "u11":false,
   "u14":true,
   "u17":false,
   "u18":true
  }
}

If I write the if statement with the hardcoded variable name such as

if obj.u10 == true

it works properly.

obj.val will literally look for a property named "val" on the object obj , as if obj = {val: true} were the object.

What you need is obj[val] which will interpolate the variable val 's actual value and use that to look up that property name

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