Angular中嵌套的foreach循环

时间:2015-03-06 06:55:06

标签: angularjs foreach

我有两个这样的数组:

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

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

我想要做的是循环浏览obj1obj2以及

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

如何以角度进行此操作?

1 个答案:

答案 0 :(得分:8)

这将是角度:

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
     }
   });
});