Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested foreach loop in Angular

I have two arrays like this:

var obj1 = [{'target_object_id': 1, 'extra': 'ok'}, {'target_object_id': 2, 'extra': 'ok'}]

var obj2 = [{'id': 4}, {'id': 2}]

What I want to do is loop through obj1 and obj2 and

if obj1.target_object_id == obj2.id // Append obj2['extra']=obj.extra

How to do this in angular?

like image 678
Joyfulgrind Avatar asked May 03 '26 03:05

Joyfulgrind


1 Answers

This would be with angular :

angular.forEach(obj1, function(item1) {
   angular.forEach(obj2, function(item2) {
    if(item1.target_object_id===item2.id) {
           item2.extra = item1.extra; // change it as you wish
           // some code
     }
   });
});
like image 128
mohamedrias Avatar answered May 05 '26 00:05

mohamedrias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!