I am using jsPdf. When a field has been left blank "undefined" is printed on the pdf. I would like to replace that with a empty string. I am trying to use a if statement but I am not getting it.
doc.text(30, 190, "Budget : $");
if ($scope.currentItem.JobOriginalBudget == "undefined") {
doc.text(50, 190, " ");
}
else {
var y = '' + $scope.currentItem.JobOriginalBudget;
doc.text(50, 190, y);
};
undefined is == only to null , and not to all other "falsy" values: 0. "" - empty string.
null. undefined (value of undefined is not the same as a parameter that was never defined) 0. "" (empty string)
There are only six falsey values in JavaScript: undefined , null , NaN , 0 , "" (empty string), and false of course.
In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.
As per this answer I believe what you want is
doc.text(50, 190, $scope.currentItem.JobOriginalBudget || " ")
undefined
is a primitive value. Instead of comparing against the identifier undefined
, you're comparing against the 9-character string "undefined
".
Simply remove the quotes:
if ($scope.currentItem.JobOriginalBudget == undefined)
Or compare against the typeof
result, which is a string:
if (typeof $scope.currentItem.JobOriginalBudget == "undefined")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With