I am new to JS, I made some investigation but couldn’t find a solution. Forgive me if it is a duplicate.
Context: I have thousands of objects, most of them the same properties. In the javascript, I get all the properties of objects and format them to show in html. Since some objects doesn’t have all the properties, i get undefined value. To solve this problem, I wrote this code:
var twitter = $.map(tweets, function(obj, index) {
return {
Format: ((typeof obj.VideographyFeatures.Format === "undefined") ? "N/A" : obj.VideographyFeatures.Format)
};
});
But it doesn’t work. For the objects which don’t have “VideographyFeatures” property, I still get the following error.
“Uncaught TypeError: Cannot read property ‘Format’ of undefined”.
My aim is to get:
{
Format: "N/A"
};
If
obj.VideographyFeaturesis undefined, you cannot get itsFormatproperty to pass to thetypeofexpression.You need to check
typeof obj.VideographyFeatures.