-6

I have two arrays like this

$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"
  }
];

I would like to compare these arrays and get the not matching values to another array like this

$scope.notmatching = [
  {
    swift_code: "00D",
    eline_description: "e-Example 3"
  }
];

So far I have tried with for each but did not work

Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
kanishka
  • 89
  • 2
  • 9
  • 4
    Please include the code you tried and how it didn't work as a [mcve] in your question. – Luca Kiebel Jun 07 '18 at 11:17
  • Please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – str Jun 07 '18 at 11:27
  • Does this answer your question? [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Vega Jul 05 '22 at 10:42

3 Answers3

1

Use Array.filter

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);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
1

you can use underscore.js that way it will be easy

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);
Rishi Raj
  • 117
  • 1
  • 7
0

You can try like the below code to get the missing record from the master to original list as shown in the below code. Also please check this plunker link for your given working example scenario.

Controller:

$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);
    });
};

Template:

<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>
Immanuel Kirubaharan
  • 1,094
  • 1
  • 11
  • 17