如何用空字符串替换undefined [英] how to replace undefined with a empty string

查看:596
本文介绍了如何用空字符串替换undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jsPdf.当字段保留为空白时,在pdf上打印"undefined".我想用一个空字符串替换它.我正在尝试使用if语句,但没有得到.

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"进行比较.

undefined is a primitive value. Instead of comparing against the identifier undefined, you're comparing against the 9-character string "undefined".

只需删除引号:

if ($scope.currentItem.JobOriginalBudget == undefined)

或与typeof结果(是字符串)进行比较:

Or compare against the typeof result, which is a string:

if (typeof $scope.currentItem.JobOriginalBudget == "undefined")

这篇关于如何用空字符串替换undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆