다음이 비어 있지 않고 비어 있는 값을 가진 배열이라고 가정해 보겠습니다. −
studentDetails[2] = "Smith";
studentDetails[3] = "";
studentDetails[4] = "UK";
function arrayHasEmptyStrings(studentDetails) {
for (var index = 0; index < studentDetails.length; index++) { 배열에 빈 문자열이 있는지 확인하는 구문은 다음과 같습니다. 이러한 확인 조건을 설정 -
if(yourArrayObjectName[yourCurrentIndexvalue]==””){
// insert your statement
} else{
// insert your statement
} 예시
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에 있습니다.