Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据多个关键点合并两个数组中的对象_Javascript - Fatal编程技术网

Javascript 根据多个关键点合并两个数组中的对象

Javascript 根据多个关键点合并两个数组中的对象,javascript,Javascript,下面我有两个数组,我想基于两个常用键(“iso3”和“year”)合并它们。为了合并,每个对象必须同时具有这两个对象。数组的长度不同 array1 = [{ "id":24006, "iso3":"AFG", "country":"Afghanistan", "year":2014, "value":29.78 }, { "id":138806, "iso3":"ALB", "country":"Albania", "year":2013, "value"

下面我有两个数组,我想基于两个常用键(“iso3”和“year”)合并它们。为了合并,每个对象必须同时具有这两个对象。数组的长度不同

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

,但数组仅基于一个公共密钥合并。我试着用那个代码工作,但没有成功。感谢您的帮助

您需要做的是扩展您所在站点的答案中提供的加入条件。像这样:

var combined = [];
function findSecond(iso3, year, second){
    for (var i = 0; i < second.length; i += 1){
    //Adding the second condition
        if (second[i].iso3 === iso3 && second[i].year ===  year ) {
            return second[i];
        }
    }
    return null;
}

while (el = array1.pop()){
    var getSec = findSecond(el.iso3, el.year, array2);
    if (getSec){
        for (var l in getSec){
            if (!(l in el)) {
                el[l] = getSec[l];
            }
        }
        combined.push(el);
    }
}
var组合=[];
功能查找秒(iso3,年,秒){
对于(变量i=0;i
另一种利用和

代码:


这种方法首先基于键创建合并对象的字典(或映射)。它使用jQuery$.extend合并单个对象(其他实用程序库可能会提供合并对象的方法)

生成字典后,它会转换回数组,并通过单个对象键进行过滤,以生成类似内部联接的结果。如果要进行外部联接,只需删除筛选器

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 dictionary 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");
});

JS Fiddle(输出记录到控制台:)

此解决方案具有一个用于收集项目的对象,然后呈现所需的数组

var array1=[{“id”:24006,“iso3”:“AFG”,“国家”:“阿富汗”,“年份”:2014,“价值”:29.78},{“id”:138806,“iso3”:“ALB”,“国家”:“阿尔巴尼亚”,“年份”:2013,“价值”:0.6341109715},{“id”:44206,“iso3”:“DZA”,“国家”:“阿尔及利亚”,“年份”:2014,“价值”:39.928947},
array2=[{“指标id”:21806,“脚注id”:64811,“iso3”:“AFG”,“年份”:2014},{“指标id”:23806,“脚注id”:15711,“iso3”:“AFG”,“年份”:2013},{“指标id”:123406,“脚注id”:15711,“iso3”:“ALB”,“年份”:2013},{“指标id”:101606,“脚注id”:48911,“iso3”:“DZA”,“年份”:2013};
函数合并(array1,array2){
函数makeObj(a){
obj[a.iso3]=obj[a.iso3]| |{};
obj[a.iso3][a.year]=obj[a.iso3][a.year]|{};
Object.keys(a).forEach(函数(k){
obj[a.iso3][a.year][k]=a[k];
});
}
变量数组=[],
obj={};
阵列1.forEach(makeObj);
array2.forEach(makeObj);
Object.keys(obj.forEach)(函数(k){
Object.key(obj[k]).forEach(function(kk){
array.push(obj[k][kk]);
});
});
返回数组;
}

write(“”+JSON.stringify(merge(array1,array2),0,4)+’
因此您需要从具有相同年份和ISO的数组中筛选出对象。是这样吗?根据您链接到的答案中的示例,编辑行
if(second[i].id==id){
to
if(second[i].iso3==iso3&second[i].year==year){
试试看。看看是否有用?@HarryBomrah是的,没错。@Irtza.QC这是我已经尝试过的,它给了我一个空白屏幕。我做了一个JSFIDLE,但出现了错误。我不确定我遗漏了什么。你能看看吗?你的小提琴工作得很好:它连接了两个记录。在这里,在结尾添加了一个简单的测试:这很完美。谢谢你太感谢你了!哇,谢谢你把我推过了15k积分的边缘;)
[{
  "indicator_id": 23806,      /* Found in array2 only, not merged */
  "footnote_id": 15711,
  "iso3": "AFG",
  "year": 2013
}, {
  "id": 138806,               /* This one has been merged */
  "iso3": "ALB",
  "country": "Albania",
  "year": 2013,
  "value": 0.6341109715,
  "indicator_id": 123406,
  "footnote_id": 15711
}, {           
  "indicator_id": 101606,     /* Found in array2 only, not merged */
  "footnote_id": 48911,
  "iso3": "DZA",
  "year": 2013
}, {
  "id": 24006,                /* This one has been merged */
  "iso3": "AFG",
  "country": "Afghanistan",
  "year": 2014,
  "value": 29.78,
  "indicator_id": 21806,
  "footnote_id": 64811
}, {
  "id": 44206,                /* Found in array1 only, not merged */
  "iso3": "DZA",
  "country": "Algeria",
  "year": 2014,
  "value": 39.928947
}]
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 dictionary 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");
});