1 回答
TA贡献1813条经验 获得超2个赞
你的第一个for loop不正确。不需要多个if elseif. 您只需要检查道具school name是否为空。
在你的forEach循环中,你已经有了一个学校对象,不需要Object.keys.
const schoolarray = [{
"school name": "first school",
"school street": "first street",
"school add": "first add"
}, {
// Empty
"school name" : "",
"school street" : "second street",
"school add" : "second add"
}, {
"school name" : "third school",
"school street" : "third street",
"school add" : "third add"
}];
if (schoolarray.length > 0 ) {
schoolarray.forEach(function(schoolObject, index) {
if(!schoolObject['school name']) {
console.log('Please enter school name', schoolObject);
} else {
console.log('School name is not empty: ', schoolObject['school name']);
}
});
}
使用 for loop
const schoolarray = [{
"school name": "first school",
"school street": "first street",
"school add": "first add"
}, {
"school name" : "",
"school street" : "second street",
"school add" : "second add"
}, {
"school name" : "third school",
"school street" : "third street",
"school add" : "third add"
}];
for (i=0; i<schoolarray.length; i++) {
if(!schoolarray[i]["school name"]) {
console.log('Please enter school name', schoolarray[i]);
} else {
console.log('School name is not empty: ', schoolarray[i]["school name"]);
}
}
PS:不要使用,alert
因为它会阻止窗口重绘。
添加回答
举报