这是我的问题:我能有这样的对象数组:检查的对象值为null或空的JavaScript
[{
"name": "Alex",
"code": "05422180283",
"pIva": "05422180283",
"subCode": null
}, {
"name": "John",
"code": null,
"pIva": null,
"subCode": "IT"
}, {
"name": "Billy",
"code": null,
"pIva": null,
"subCode": "IT"
}, {
"name": "Tom",
"code": null,
"pIva": null,
"subCode": "IT"
}]
这JSON是一个形式由ng-repeat
使用和空场应填写。当提交按钮被按下时,系统应该检查是否仍有空的字段。所以现在的情况可能是这样的:
[{
"name": "Alex",
"code": "05422180283",
"pIva": "05422180283",
"subCode": null
}, {
"name": "John",
"code": "88985556",
"pIva": "1919ASVVV",
"subCode": "9991VVVV"
}, {
"name": "Billy",
"code": "89952366555",
"pIva": "BB588918989",
"subCode": "ASA234434"
}, {
"name": "Tom",
"code": null,
"pIva": "541198198",
"subCode": "ACEVV9999"
}]
正如你可以看到有2个空字段。在这种情况下,系统应该建议用户有两个空的字段。 (带有警报或类似的东西)。否则,用户可以毫无问题地提交表单。这是我迄今为止所做的:
angular.forEach($scope.myArray, function(items){
if(_.chain(items).find(_.isNull).isNull().value()) {
//console.log('Found one');
formHasEmptyFields = true;
} else {
//console.log('Did not find one');
formHasEmptyFields = false;
}
if(_(items).find(_.isNull) === null) {
//console.log('Found one');
formHasEmptyFields = true;
} else {
//console.log('Did not find one');
formHasEmptyFields = false;
}
});
if(formHasEmptyFields == true) {
alert('There are empty fields');
} else {
submitFunction($scope.myArray);
}
但它不起作用,因为它似乎只检查最后一个对象。事实上,如果我填写表单中的最后一个对象,它不会向我显示警报。任何想法? $scope.myArray
是我的当然对象的数组
能不能请你附上你的html表格 –