从输出中删除字符串“ undefined”

时间:2018-07-02 09:01:28

标签: javascript jquery

[MLModel compileModelAtURL:modelURL error&error]

在这里我 想要

首先检查 <script> var names = <?php print_r(json_encode($_SESSION['name'])); ?>; var lnames = <?php print_r(json_encode($_SESSION['lname'])); ?>; var j=1; $("#add_driver").click(function () { $( "#add_driver_section").replaceWith( "<div class='wrap-input100 validate-input bg1 rs1-wrap-input100' data-validate = 'Enter Your First Name'> <span class='label-input100'>Firstname *</span> <input class='input100' type='text' name='name[]' placeholder='Enter Your First Name ' value='"+ names[j] +"'></div><div class='wrap-input100 validate- input bg1 rs1-wrap-input100' data-validate = 'Enter Your Last Name'> <span class='label-input100'>Lastname *</span> <input class='input100' type='text' name='lname[]' placeholder='Enter Your Last Name ' value='"+ lnames[j] +"'></div>"); j++;} ); </script> 是否为空,并且仅在不为空时使用它。当前,当我运行我的代码时,如果names[j]为空,则会得到字符串“ undefined”作为输出。 简单的方法是什么? the output i am getting

2 个答案:

答案 0 :(得分:2)

只需:

let mystring = arr[i] || '';

在您的情况下:

let str = "start of string" + (arr[i] || '') + "end of string";

答案 1 :(得分:1)

您可以使用PHP检查$_SESSION[]是否为空:

$names = isset($_SESSION['name']) ? $_SESSION['name'] : [] ;
$lnames = isset($_SESSION['lname']) ? $_SESSION['lname'] : [] ;

var names = <?php echo (json_encode($names)); ?>;
var lnames = <?php echo (json_encode($lnames)); ?>;