猿问

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

我有一个对象数组——带有行的表(每一行都是一个对象)


试图验证对象数组。如果我在中间行有错误,则不显示任何错误消息。如果我在最后一行有错误,它会显示。请帮帮我。


我试过循环:


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');

            }   

        });

    });

}


至尊宝的传说
浏览 134回答 1
1回答

慕姐8265434

你的第一个for loop不正确。不需要多个if elseif. 您只需要检查道具school name是否为空。在你的forEach循环中,你已经有了一个学校对象,不需要Object.keys.const schoolarray = [{&nbsp; &nbsp; "school name": "first school",&nbsp; &nbsp; "school street": "first street",&nbsp; &nbsp; "school add": "first add"&nbsp; }, {&nbsp; &nbsp; // Empty&nbsp; &nbsp; "school name" : "",&nbsp; &nbsp; "school street" : "second street",&nbsp; &nbsp; "school add" : "second add"&nbsp; }, {&nbsp; &nbsp; "school name" : "third school",&nbsp; &nbsp; "school street" : "third street",&nbsp; &nbsp; "school add" : "third add"&nbsp; }];if (schoolarray.length > 0 ) {&nbsp; &nbsp; schoolarray.forEach(function(schoolObject, index) {&nbsp; &nbsp; &nbsp; &nbsp; if(!schoolObject['school name']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('Please enter school name', schoolObject);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('School name is not empty: ', schoolObject['school name']);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}使用 for loopconst schoolarray = [{&nbsp; &nbsp; "school name": "first school",&nbsp; &nbsp; "school street": "first street",&nbsp; &nbsp; "school add": "first add"&nbsp; }, {&nbsp; &nbsp; "school name" : "",&nbsp; &nbsp; "school street" : "second street",&nbsp; &nbsp; "school add" : "second add"&nbsp; }, {&nbsp; &nbsp; "school name" : "third school",&nbsp; &nbsp; "school street" : "third street",&nbsp; &nbsp; "school add" : "third add"&nbsp; }];for (i=0; i<schoolarray.length; i++) {&nbsp; &nbsp; if(!schoolarray[i]["school name"]) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('Please enter school name', schoolarray[i]);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; console.log('School name is not empty: ', schoolarray[i]["school name"]);&nbsp;&nbsp;&nbsp; &nbsp; }}PS:不要使用,alert因为它会阻止窗口重绘。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答