I am facing a problem about object comparison.
I have two objects list in javascript. How should i filter out the data if object properties is exactly same, then copy to new scope object. Any way to archive this if using AngularJs, Javascript or Underscorejs ? I already put into 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);
}
I think use the underscoreJs is the best way to handle such as simple code syntax. First i use some method of underscorejs such as _.find, then handle the logic.
angular.forEach(obj1, function(v, k) {
var isFind = _.find(obj2, function(o) {
// some logic.
})
})