我已经做了以下工作,循环遍历AngularJS中的两个对象列表,并有条件地创建一个新的对象列表。
$scope.loading = true;
$scope.toUpdate = [];
for (var i = 0; i < changedItems.length; i++) {
for (var j = 0; j < $scope.originalItems.length; j++) {
if (changedItems[i].AttributeName == $scope.originalItems[j].AttributeName
&& changedItems[i].Value != $scope.originalItems[j].Value) {
$scope.toUpdate.push(changedItems[i]);
}
}
}
当值被用户更改为时,我正在创建一个对象列表( $scope.originalItems
有原始值)。
我将其发送到MVC控制器以更新更改。
我这样做是因为我不需要更新那些没有被更改的值。
这种方法适用于许多迭代。
要求:
我确信这可以是,直接使用Underscore.js的,但我无法做到。
需要帮助将此逻辑转换为下划线。
发布于 2014-09-30 00:45:59
您的代码可以转换为这样的下划线:
_.each(item,function(i){
_.each($scope.previous,function(p) {
if (i.AttributeName == p.AttributeName && i.Value != p.Value)
$scope.toUpdate.push(i);
});
});
但我认为你想避免重复的项目,然后你可以试试这个:
$scope.toUpdate = _.filter(item,function(i) {
return _.any($scope.previous,function(p){
return i.AttributeName == p.AttributeName && i.Value != p.Value;
});
});
更新
https://stackoverflow.com/questions/26116298
复制相似问题