2

I'm trying to iterate through a list of objects in my lovely Angular/Typescript code, but for some reason it doesn't even attempt to do so. Here's the code:

businessList: RemoteDataSet<BusinessModel>;
businessModel: BusinessModel; 

this.businessList.forEach( bl => {
            console.log("in foreach");
            if (bl.id == this.user.businessId) {
                console.log("they are equal");
                this.businessModel = bl;
            }
            else {
                console.log("They are not equal");
            }
        });

I've verified that this.businessList has data (about 40 items). Even so, it doesn't iterate through even once. I'm obviously pretty new to Angular and Typescript, but this seems right. What am I missing?

3
  • 3
    Post a complete minimal example reproducing the problem. In particular, post the code of RemoteDataSet and its forEach() method. We can't possibly guess how it's implemented. Commented Apr 3, 2017 at 19:38
  • 1
    If you could also post a sample of your JSON BusinessModel objects it would be very helpful. Commented Apr 3, 2017 at 19:42
  • You incorrectly invoked the angular foreach loop. angular.forEach(businessList =>{...} is what it should be. Check this previous question out: stackoverflow.com/questions/29953198/foreach-loop-in-angularjs Commented Apr 3, 2017 at 19:54

1 Answer 1

1

Maybe try the angular.forEach method?

    forEachFunction = () => {
        angular.forEach(this.businessList, (value, key) => {
            if (value.id == this.user.businessId) {
                console.log("they are equal");
                this.businessModel = value;
            }
            else {
                console.log("They are not equal");
            }
        });
    };
Sign up to request clarification or add additional context in comments.

Comments

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.