0

That's my problem: I could have this array of objects:

[{
    "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"
}]

This json is used in a form by an ng-repeat and the null field should be filled. When the submit button is pressed the system should be check if there are still empty field. So the situation could be this one now:

[{
    "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"
}]

As you can see there are 2 null fields. In this situation the system should be advise the user that there are 2 empty fields. (with an alert or something like that). Otherwise the user can submit the form without any problem. This is what i've done so far:

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

But it doesn't work because seems that it check only the last object. In fact, if I fill the last object in the form it doesn't show me the alert. Any idea? $scope.myArray is my array of objects of course

1
  • can you please attach your html form aswell Commented Jun 6, 2017 at 8:59

2 Answers 2

1

The problem is that although formHasEmptyFields does get set to true when there is a null, it gets overwritten with false again in the next iteration.

So, to solve this, only set formHasEmptyFields = false; once, before the loop, and inside the loop, only keep the code that sets it to true.

Having said that, a some or every method is more useful for this scenario than a forEach loop. You may want to use that instead.

Sign up to request clarification or add additional context in comments.

2 Comments

So, you mainly mean to delete the else conditions?
Indeed, drop them.
1

See the MDN docs for some

const hasItemWhithFalsyCode = items.some(x => !x.code);

You can do all sorts of stuff, like filter out the items without a code

const itemsWithTruthyCode = items.filter(x => x.code);

To explictly check for null

const hasItemWhithNullCode = items.some(x => x.code === null);

1 Comment

Indeed, using some is better than forEach in this scenario: it exits the loop as soon as there is a certain condition. It would be useful for the OP if you would provide the code specifically for their case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.