gpt4 book ai didi

javascript - 如何在 Javascript 或 Angularjs 中找到相等的对象?

转载 作者:行者123 更新时间:2023-12-01 03:53:33 25 4
gpt4 key购买 nike

我遇到了有关对象比较的问题。

我在 JavaScript 中有两个对象列表。如果对象属性完全相同,我应该如何过滤掉数据,然后复制到新的范围对象。如果使用 AngularJsJavascriptUnderscorejs ,有什么方法可以存档此内容吗?我已经放入jsfiddle

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com