admin管理员组

文章数量:1291197

In typescript I defined:

class SubjectService implements ISubjectService {

    subject: any;
    subjectId: number = 0;
    subjects = {
        "1": { "id": 1, "name": "Java" },
        "100": { "id": 100, "name": "Test" }
    };

    static $inject = [
        "$http",
        "appConstant",
    ];

    constructor(
        public $http: ng.IHttpService,
        public ac: IAppConstant
        ) {
    }

}

I then in my constructor have this code:

class SubjectController {

    static $inject = [
        "$scope",
        "subjectService"
    ];

    constructor(
        public $scope,
        public su: ISubjectService
        ) {
        $scope.su = su;

        $scope.rowClicked = (subject, $index) => {
            var self = this;
            if (subject.current && subject.current == true) {
                return;
            }
            su.subjects.forEach(function (subject) {
                subject.current = false;
            });
            su.subject = subject;
            su.subject.current = true;
        }

    }

}

But when this runs I am getting a message saying:

TypeError: su.subjects.forEach is not a function at Scope.SubjectController.$scope.rowClicked (http://localhost:1810/app/controllers/SubjectController.js:12:25)

Does anyone have any idea what might be wrong. I used similar code in other places but here it fails each time.

In typescript I defined:

class SubjectService implements ISubjectService {

    subject: any;
    subjectId: number = 0;
    subjects = {
        "1": { "id": 1, "name": "Java" },
        "100": { "id": 100, "name": "Test" }
    };

    static $inject = [
        "$http",
        "appConstant",
    ];

    constructor(
        public $http: ng.IHttpService,
        public ac: IAppConstant
        ) {
    }

}

I then in my constructor have this code:

class SubjectController {

    static $inject = [
        "$scope",
        "subjectService"
    ];

    constructor(
        public $scope,
        public su: ISubjectService
        ) {
        $scope.su = su;

        $scope.rowClicked = (subject, $index) => {
            var self = this;
            if (subject.current && subject.current == true) {
                return;
            }
            su.subjects.forEach(function (subject) {
                subject.current = false;
            });
            su.subject = subject;
            su.subject.current = true;
        }

    }

}

But when this runs I am getting a message saying:

TypeError: su.subjects.forEach is not a function at Scope.SubjectController.$scope.rowClicked (http://localhost:1810/app/controllers/SubjectController.js:12:25)

Does anyone have any idea what might be wrong. I used similar code in other places but here it fails each time.

Share Improve this question edited Jul 1, 2015 at 13:34 Cerbrus 73k19 gold badges136 silver badges150 bronze badges asked Jul 1, 2015 at 13:26 Samantha J T StarSamantha J T Star 32.8k89 gold badges256 silver badges441 bronze badges 1
  • 1 possible duplicate of Why is "forEach not a function" for this map? – Cerbrus Commented Jul 1, 2015 at 13:35
Add a ment  | 

3 Answers 3

Reset to default 3

subjects is an Object, not an Array, because of its {} notation. You can loop through 1 and 100 as keys if you like using Object.keys(subjects). You could also start it out as an empty array ([]) and then set the values of subjects[1] and subjects[100] if you like, but there's no shorthand inline way to just define two separated indices of an array without the inbetween ones.

su.subjects is an object, not an array and forEach is not defined for Object.

I would also note that forEach is not implemented in all versions of browsers (looking at you, IE 8) since forEach was not included until ECMA 5.1. See forEach

本文标签: javascriptWhy do I get a message saying forEach is not a functionStack Overflow