Angular ForEach in Angular4/Typescript?
Asked Answered
S

5

32

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?

Sinister answered 12/6, 2017 at 14:43 Comment(2)
_this.hierarchicalData.forEach should work.. why you need angular.forEach here. native is best :pEbonize
try Array.forEachPetrolic
V
60

in angular4 foreach like that. try this.

 selectChildren(data, $event) {
      let parentChecked = data.checked;
       this.hierarchicalData.forEach(obj => {
          obj.forEach(childObj=> {
            value.checked = parentChecked;
         });
      });
    }
Valiancy answered 12/6, 2017 at 14:47 Comment(2)
You can choose after set seeing this article.https://mcmap.net/q/108613/-angular-js-break-foreachSkipjack
forEach, uppercase E. :-)Avigdor
L
15

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;
      }
   }
}
Logarithm answered 12/6, 2017 at 14:51 Comment(1)
for is not TypeScript. It's JavaScript.Peddler
P
5
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;
                            }
                        })
                    })
Philippe answered 4/4, 2018 at 10:55 Comment(0)
S
1
    data.forEach((element: any) => {
this.currencies = element;
}
Sacaton answered 13/5 at 15:9 Comment(0)
I
-1

In Typescript use the For Each like below.

selectChildren(data, $event) {
let parentChecked = data.checked;
for(var obj in this.hierarchicalData)
    {
        for (var childObj in obj )
        {
            value.checked = parentChecked;
        }
    }
}
Impaste answered 17/12, 2018 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.