I am iterating over a list of objects of mixed types. For each object I access a bunch of properties. Some of these objects will not have some of the properties. Is there a way to avoid using object.hasProperty method to safely access nonexisting properties?
Asked
Active
Viewed 945 times
2 Answers
1
You could iterate through the object's properties.
The following link might help for this :-
-
yep, this would be the way I'd go: you have the list of properties you want to get values for (if they exist), then get the list of properties on the object and iterate over the intersection of the two lists. – Ted Naleid May 21 '12 at 16:13
-
thanks for answering. I actually like the way the person asking the question in that link us doing it, since I need a subset of properties ... hmmm, if only we had safety operator for properties (or similar way the JS has a truthy return on property access) – dbrin May 21 '12 at 18:32
0
looks like the only reasonable way is to use hasProperty method for sanity check

dbrin
- 15,525
- 4
- 56
- 83