我有两个像这样的数组
$scope.eClauses = [
{
swift_code: "31D",
eline_description: "e-Example 1"
},
{
swift_code: "41D",
eline_description: "e-Example 2"
},
{
swift_code: "00D",
eline_description: "e-Example 3"
}
];
$scope.masterClauses = [
{
swift_code: "31D",
eline_description: "e-Example 1"
},
{
swift_code: "41D",
eline_description: "e-Example 2"
}
];
我想比较这些数组,并将不匹配的值与另一个数组(如
)相匹配$scope.notmatching = [
{
swift_code: "00D",
eline_description: "e-Example 3"
}
];
到目前为止,我已尝试使用进行每次,但无法正常工作
答案 0 :(得分:1)
使用here
let eClauses =[{swift_code:"31D",eline_description:"e-Example1"},{swift_code:"41D",eline_description:"e-Example2"},{swift_code:"00D",eline_description:"e-Example3"}];
let masterClauses = [{swift_code:"31D",eline_description:"e-Example1"},{swift_code:"41D",eline_description:"e-Example2"}];
// Create an array of unique identifier for the objects in masterClause
let keys = Object.keys(masterClauses.reduce((a, {swift_code, eline_description}) => Object.assign(a, {[swift_code + "_" + eline_description] : undefined}), {}));
// Filter in those elements which are missing from the above array
let filtered = eClauses.filter(({swift_code, eline_description}) => !keys.includes(swift_code + "_" + eline_description))
console.log(filtered);
答案 1 :(得分:1)
你可以使用underscore.js这样很容易
var c = _.difference($scope.eClauses.map(e => e.swift_code), $scope.masterClauses.map(e =>e.swift_code));
var array = [];
array = $scope.eClauses.map(e => {
if(c.includes(e.swift_code)){
return e;
}
}).filter(r=>r);
答案 2 :(得分:0)
您可以尝试使用以下代码来获取从主列表到原始列表的缺失记录,如下面的代码所示。另请查看您的特定工作示例方案的this plunker链接。
<强>控制器:强>
$scope.getMissingRecord=function(){
angular.forEach($scope.eClauses, function(item){
var index = $scope.masterClauses.map(function(e){ return e.eline_description; }).indexOf(item.eline_description);
if(index==-1) $scope.notmatching.push(item);
});
};
模板:
<button type="button" ng-click="getMissingRecord();">Click To Get Unmatched Clauses</button>
<ul ng-repeat="um in notmatching">
<li>{{um.swift_code}}</li>
<li>{{um.eline_description}}</li>
</ul>