作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了有关对象比较的问题。
我在 JavaScript 中有两个对象列表。如果对象属性完全相同,我应该如何过滤掉数据,然后复制到新的范围对象。如果使用 AngularJs、Javascript 或 Underscorejs ,有什么方法可以存档此内容吗?我已经放入jsfiddle
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.obj1 = [];
$scope.obj2 = [];
$scope.obj1.push({
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "22",
style: null,
discPer: 10
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "24",
style: null,
discPer: 20
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "80",
style: null,
discPer: 15
});
$scope.obj2.push({
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "81",
style: null,
discPer: 10
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "24",
style: null,
discPer: 20
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "80",
style: null,
discPer: 15
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "81",
style: null,
discPer: 8
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "83",
style: null,
discPer: 25
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "84",
style: null,
discPer: 30
});
console.log($scope.obj1);
console.log($scope.obj2);
}
我认为使用underscoreJs是处理诸如简单代码语法的最佳方式。首先我使用underscorejs的一些方法,例如_.find,然后处理逻辑。
angular.forEach(obj1, function(v, k) {
var isFind = _.find(obj2, function(o) {
// some logic.
})
})
最佳答案
创建一个过滤器来检查对象并使用 javascript filter 和 find
函数返回它
var arr = $scope.obj1.filter(o=> $scope.obj2.find(f=> JSON.stringify(o) === JSON.stringify(f) ));
console.log(arr)
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.obj1 = [];
$scope.obj2 = [];
$scope.obj1.push({
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "22",
style: null,
discPer: 10
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "24",
style: null,
discPer: 20
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "80",
style: null,
discPer: 15
});
$scope.obj2.push({
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "81",
style: null,
discPer: 10
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "24",
style: null,
discPer: 20
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "80",
style: null,
discPer: 15
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "81",
style: null,
discPer: 8
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "83",
style: null,
discPer: 25
},{
buCode: "1000",
deptCode: "1201",
brandCode: null,
prodType: 'C',
cardType: "84",
style: null,
discPer: 30
});
var arr = $scope.obj1.filter(o=> $scope.obj2.find(f=> JSON.stringify(o) === JSON.stringify(f) ));
console.log(arr)
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
</div>
关于javascript - 如何在 Javascript 或 Angularjs 中找到相等的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42974863/
我是一名优秀的程序员,十分优秀!