Angular ForEach in Angular4/Typescript?

251,772

Solution 1

in angular4 foreach like that. try this.

 selectChildren(data, $event) {
      let parentChecked = data.checked;
       this.hierarchicalData.forEach(obj => {
          obj.forEach(childObj=> {
            value.checked = parentChecked;
         });
      });
    }

Solution 2

you can try typescript's For :

selectChildren(data , $event){
   let parentChecked : boolean = data.checked;
   for(let o of this.hierarchicalData){
      for(let child of o){
         child.checked = parentChecked;
      }
   }
}

Solution 3

arrayData.forEach((key : any, val: any) => {
                        key['index'] = val + 1;

                        arrayData2.forEach((keys : any, vals :any) => {
                            if (key.group_id == keys.id) {
                                key.group_name = keys.group_name;
                            }
                        })
                    })
Share:
251,772

Related videos on Youtube

Steve
Author by

Steve

Updated on January 25, 2020

Comments

  • Steve
    Steve over 4 years

    I see many answers about using ngFor when I search for this, but I understand ngFor. I'm asking about the angular.forEach() constructor used in my Angular 1 controllers. These are flagged as errors in TS and do not compile.

    For example, I have one with a nested loop:

     _this.selectChildren = function (data, $event) {
      var parentChecked = data.checked;
      angular.forEach(_this.hierarchicalData, function (value, key) {
        angular.forEach(value.children, function (value, key) {
          value.checked = parentChecked;
        });
      });
    };
    

    What does this construct look like in Typescript for Angular 4?

    • Pankaj Parkar
      Pankaj Parkar almost 7 years
      _this.hierarchicalData.forEach should work.. why you need angular.forEach here. native is best :p
    • anoop
      anoop almost 7 years
  • Admin
    Admin almost 7 years
    for is not TypeScript. It's JavaScript.
  • Ranjithkumar
    Ranjithkumar about 6 years
    You can choose after set seeing this article.stackoverflow.com/questions/13843972/angular-js-brea‌​k-foreach
  • Mr. B.
    Mr. B. about 5 years
    forEach, uppercase E. :-)