假设以下是带有非空和空值的数组-
studentDetails[2] = "Smith"; studentDetails[3] = ""; studentDetails[4] = "UK"; function arrayHasEmptyStrings(studentDetails) { for (var index = 0; index < studentDetails.length; index++) {
要检查数组是否为空字符串,语法如下。设置检查条件-
if(yourArrayObjectName[yourCurrentIndexvalue]==””){ //插入您的声明 } else{ //插入您的声明 }
var studentDetails = new Array(); studentDetails[0] = "John"; studentDetails[1] = ""; studentDetails[2] = "Smith"; studentDetails[3] = ""; studentDetails[4] = "UK"; function arrayHasEmptyStrings(studentDetails) { for (var index = 0; index < studentDetails.length; index++) { if (studentDetails[index] == "") console.log("The array has empty strings at the index=" + (index)); else console.log("The value is at index="+(index)+"="+studentDetails[index]); } } arrayHasEmptyStrings(studentDetails);
要运行以上程序,您需要使用以下命令-
node fileName.js
在这里,我的文件名为demo210.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\javascript-code> node demo210.js The value is at index=0=John The array has empty strings at the index=1 The value is at index=2=Smith The array has empty strings at the index=3 The value is at index=4=UK