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;
}
})
})
Related videos on Youtube
Author by
Steve
Updated on January 25, 2020Comments
-
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 almost 7 years
_this.hierarchicalData.forEach
should work.. why you need angular.forEach here. native is best :p -
anoop almost 7 yearstry Array.forEach
-
-
Admin almost 7 years
for
is not TypeScript. It's JavaScript. -
Ranjithkumar about 6 yearsYou can choose after set seeing this article.stackoverflow.com/questions/13843972/angular-js-break-foreach
-
Mr. B. about 5 years
forEach
, uppercaseE
. :-)