Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  136] [ 2]  / answers: 1 / hits: 23699  / 8 Years ago, wed, march 2, 2016, 12:00:00

I am wanting to store TaskPaper-like tasks in a JavaScript object. Suppose I have a task like



- Post to StackOverflow @priority(2) @due(2016-05-03) @flagged


This would turn into



{name:Post to StackOverflow, priority: 2, due:2016-05-03, flagged:value}


Flagged doesn't need a value and all I can think of is to use true or null



I would like to later filter tasks using if key in object and having true/false values wouldn't work with my workflow.



What would the SO community recommend as best practices.


More From » json

 Answers
0

If you do something like if (myobj.flagged), then if your value of flagged is true, the test passes. If the flagged property doesn't exist, then you get undefined which is falsy. So it works with flagged:true, flagged:false and even no flagged property. So use true, and omit the property altogether when it's not supposed to be there (so for...in would work in that case).


[#63077] Tuesday, March 1, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andrewb

Total Points: 259
Total Questions: 124
Total Answers: 90

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;