如何检查JavaScript数组是否为空字符串?
假设以下是带有非空和空值的数组-
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
热门推荐
10 上嫂子生日祝福语简短
11 直播生孩子祝福语简短
12 今日祝福语简短10字
13 老师送的祝福语简短
14 简短长辈新年祝福语
15 祝寿时祝福语简短精辟
16 对离岗同事祝福语简短
17 顾客简短祝福语大全集
18 生日祝福语 简短独特高考