为了账号安全,请及时绑定邮箱和手机立即绑定

不在数组验证中显示对象的所有元素,只在 javascript 中显示元素的最后一个变量

不在数组验证中显示对象的所有元素,只在 javascript 中显示元素的最后一个变量

至尊宝的传说 2021-06-08 17:00:01
我有一个对象数组——带有行的表(每一行都是一个对象)试图验证对象数组。如果我在中间行有错误,则不显示任何错误消息。如果我在最后一行有错误,它会显示。请帮帮我。我试过循环:for (i=0, i<schoolarray.length; i++) {    if (schoolarray[i]['school name'] = "") {       alert('school name is empty');    }    else if (schoolarray[i]['school name'] != "") {        alert('school name is not empty');    }   if (schoolarray[i]['school street'] = "") {       alert('school street is empty');    }    else if (schoolarray[i]['school street'] != "") {        alert('school street is not empty');    }    if (schoolarray[i]['school add'] = "") {       alert('school addis empty');    }    else if (schoolarray[i]['school add'] != "") {        alert('school add is not empty');    }}错误消息显示不正确...不同的迭代得到不同的元素消息!在下面尝试了 foreach 循环:类似的问题:有人建议我吗?schoolarray = [ { 学校名称:“第一学校”,学校街道:“第一街道”,学校地址:“第一学校”},{学校名称:“第二学校”,学校街道:“第二街道”,学校地址:“第二添加"},{学校名称:"第三学校",学校街道:"第三街",学校添加:"第三添加"}]  if (schoolarray.length > 0 ) {    schoolarray.forEach(function(schoolObject, index) {        console.log(schoolObject['school name']);         Object.keys(schoolObject).forEach(function(prop) {                if(schoolObject['school name'] == "" ) {            alert('enter school');            }            else if (schoolObject['school name'] != "" ) {                alert('good');            }           });    });}
查看完整描述

1 回答

?
慕姐8265434

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因为它会阻止窗口重绘。


查看完整回答
反对 回复 2021-06-18
  • 1 回答
  • 0 关注
  • 133 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信