-5

//From these two arrays i have to get matched result & unmached result

var array1 = ["2017-07-23_30-12-98","2016-06-23_13-12-23","2017-05-20_30-12-43","2015-02-23_30-12-98"];
var array2 = ["2017-07-23_30-12-98","2014-06-23_13-12-94","2015-05-20_30-12-98","2015-02-23_30-12-98"];

result

MatchedRes = ["2017-07-23_30-12-98","2015-02-23_30-12-98"];
UnMatchedRes = ["2016-06-23_13-12-23","2017-05-20_30-12-43"];



Thanks
Sreeram
Scott Marcus
  • 60,351
  • 6
  • 44
  • 64
sreeram k
  • 15
  • 4
  • 1
    Possible duplicate of [Simplest code for array intersection in javascript](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – Scott Marcus Jul 24 '17 at 19:35
  • So loop over the one and see if it has a match in the other. If it does, add to array one, if not add to the other..... – epascarello Jul 24 '17 at 19:35

1 Answers1

0

var array1 = ["2017-07-23_30-12-98","2016-06-23_13-12-23","2017-05-20_30-12-43","2015-02-23_30-12-98"];
var array2 = ["2017-07-23_30-12-98","2014-06-23_13-12-94","2015-05-20_30-12-98","2015-02-23_30-12-98"];

console.log(
  array1.reduce((obj, s) => {
    if(array2.indexOf(s) !== -1) {
      obj.matched.push(s);
    } else {
      obj.unmatched.push(s);
    }
    return obj;
  }, {matched: [], unmatched: []})
)
Psidom
  • 195,464
  • 25
  • 298
  • 322