如何检查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 老婆生日祝福语短句简短