I have a problem and I hope you kind JavaScript people on Stack Overflow can help me out with this.
Given the following arrays:
var array1 = [{address: "www.example.com", hits: 1}, {address: "www.anotherone.org", hits: 10}]
var array2 = [{address: "www.example.com", buttons: 1}, {address: "www.yetanotherone.info", buttons: 2}]
var array3 = [{address: "www.example.com", cons: 1}, {address: "andevenonemore.com", cons: 1}]
I want to merge these array by the address key, that will be common in all arrays but may differ in value. If a address value is not in an array, it should default to 0. So, the result array should look like this:
var resultArray = [{
address: "www.example.com",
hits: 1,
buttons: 1,
cons: 1
}, {
address: "www.anotherone.org",
hits: 1,
buttons: 0,
cons: 0
}, {
address: "www.yetanotherone.info",
hits: 0,
buttons: 2,
cons: 0
}, {
address: "andevenonemore.com",
hits: 0,
buttons: 0,
cons: 1
}]
The merge will happen on the server, and I have the possibility to use Underscore.js.
I really hope, somebody much smarter than me can give me an efficient way to solve this. Really appreciate your help!