Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I have two arrays, below, which I'd like to merge based on two common keys ("iso3" and "year"). Each object must have both in common in order to be merged. The arrays are not the same length.

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
}]

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

I saw this answerI saw this answer, but the arrays are merged based on only one common key. I tried to work from that code, but I didn't get anywhere. Any help is appreciated!

I have two arrays, below, which I'd like to merge based on two common keys ("iso3" and "year"). Each object must have both in common in order to be merged. The arrays are not the same length.

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
}]

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

I saw this answer, but the arrays are merged based on only one common key. I tried to work from that code, but I didn't get anywhere. Any help is appreciated!

I have two arrays, below, which I'd like to merge based on two common keys ("iso3" and "year"). Each object must have both in common in order to be merged. The arrays are not the same length.

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
}]

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

I saw this answer, but the arrays are merged based on only one common key. I tried to work from that code, but I didn't get anywhere. Any help is appreciated!

Source Link
sprucegoose
  • 602
  • 13
  • 31

merge objects from two arrays depending on multiple keys

I have two arrays, below, which I'd like to merge based on two common keys ("iso3" and "year"). Each object must have both in common in order to be merged. The arrays are not the same length.

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
}]

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

I saw this answer, but the arrays are merged based on only one common key. I tried to work from that code, but I didn't get anywhere. Any help is appreciated!