JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
JavaScript
var array1 = [{
"id":24006,
"iso3":"AFG",
"country":"Afghanistan",
"year":2014,
"value":29.78
},
{
"id":138806,
"iso3":"ALB",
"country":"Albania",
"year":2013,
"value":0.6341109715
},
{
"id":44206,
"iso3":"DZA",
"country":"Algeria",
"year":2014,
"value":39.928947
}]
var array2 = [{
"indicator_id":21806,
"footnote_id":64811,
"iso3":"AFG",
"year":2014
},
{
"indicator_id":23806,
"footnote_id":15711,
"iso3":"AFG",
"year":2013
},
{
"indicator_id":123406,
"footnote_id":15711,
"iso3":"ALB",
"year":2013
},
{
"indicator_id":101606,
"footnote_id":48911,
"iso3":"DZA",
"year":2013
}];
// merge by key where possible
var dict = array1.concat(array2).reduce(function (dict, val) {
// Use the two values we want to join on as a key
var key = val.iso3 + val.year;
// Lookup existing object with same key, default to
// empty object if one does not exist
var existing = dict[key] || {};
// Merge two objects
dict[key] = $.extend(existing, val);
return dict;
}, {});
var joined = Object.keys(dict).map(function (key) {
// pull the dict values back out into an array
return dict[key];
}).filter(function (obj) {
// this is making it work like an inner out, an outer join can be
// done by removing the filter
return (typeof obj.indicator_id !== "undefined") && (typeof obj.country !== "undefined");
});
joined.forEach(function (x) { console.log(x); });