-5

I need to match two array and merge by matching key value using JavaScript/Node.js. Here is my code:

var userData=[{'email':'a@gmail.com','name':'Raj'},{'email':'b@gmail.com','name':'Rahul'}];
var userData1=[{'email':'a@gmail.com','address':'abcdf'},{'email':'b@gmail.com','address':'bbsr'}];

Here I have two array and I need to merge both array by matching the email value and the expected output is like below.

var finalArr=[{'email':'a@gmail.com','name':'Raj','address':'abcdf'},{'email':'b@gmail.com','name':'Rahul','address':'bbsr'}];
halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • Possible duplicate of [How to merge two arrays in Javascript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – M3talM0nk3y Aug 24 '17 at 02:52
  • What should happen if there is an entry in one of the arrays without a corresponding entry in the other? – nnnnnn Aug 24 '17 at 03:16

2 Answers2

0
var finalArr = [];
userData.map(item => {
  userData1.map(item1 => {
    if (item.email === item1.email) {
      finalArr.push(Object.assign(item, item1));
    }
  })
})

It's not so difficult, you should think youself next time.

kaijun
  • 11,376
  • 1
  • 9
  • 12
  • 1
    `if (item.email === item1.emal) {...}` – smac89 Aug 24 '17 at 02:57
  • You're misusing the `.map()` method. If you just want to iterate use `.forEach()`. If you actually want to map then use `.map()` and make sure your callback returns a value and that the return value from `.map()` is used. – nnnnnn Aug 24 '17 at 03:15
  • @nnnnnn - there are benchmarks that supposedly show `.map` is significantly faster than `.forEach` - which is why so many code examples misuse `.map` – Jaromanda X Aug 24 '17 at 03:38
  • @JaromandaX - Really? That's weird. Even though a nested `.map()` creates a bunch of arrays that get thrown away? Either way it makes the code harder to read. – nnnnnn Aug 24 '17 at 03:41
  • I said "supposedly", I haven't bothered benchmarking this myself, but have read it multiple times - here's one https://jsperf.com/native-map-versus-array-looping - forEach is 12% slower than map - however, you should note that the benchmark tests using .map vs using forEach to do what .map does – Jaromanda X Aug 24 '17 at 03:43
  • 1
    if you look at https://jsperf.com/native-map-versus-array-looping/24 - then you can see using `.map` for what `.forEach` does, `.forEach` is twice as fast in Chrome and almost 10x faster in Firefox - it doesn't help when you read numpties say things like "I advise always using map" like this numpty did in the past - https://ryanpcmcquen.org/javascript/2015/10/25/DEPRECATED-map-vs-foreach-vs-for.html – Jaromanda X Aug 24 '17 at 03:53
0

You can do it by using underscore

var mergedArray = _.map(userData, function(element){
    let findItem = _.findWhere(userData1, {email: element.email})
    return _.extend(element, findItem);
});

you can check fiddle

Anshuman Jaiswal
  • 5,352
  • 1
  • 29
  • 46