這是我的問題:我能有這樣的對象數組:檢查的對象值爲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表格 –