Javascript 如何基于不同索引合并两个对象

Javascript 如何基于不同索引合并两个对象,javascript,object,lodash,Javascript,Object,Lodash,我在一个API端点返回的数组中有两个对象,该端点根据区域设置公开当选官员。一个是办公室,另一个是官员。我需要了解如何将这两个对象合并为一个更有意义的对象 { "offices": [ { "name": "President of the United States", "officialIndices": [0] }, { "name": "Vice-President of the United States", "offi

我在一个API端点返回的数组中有两个对象,该端点根据区域设置公开当选官员。一个是
办公室
,另一个是
官员
。我需要了解如何将这两个对象合并为一个更有意义的对象

{
  "offices": [
    {
      "name": "President of the United States",
      "officialIndices": [0]
    }, {
      "name": "Vice-President of the United States",
      "officialIndices": [1]
    }, {
      "name": "United States Senate",
      "officialIndices": [2, 3]
    }, {
      "name": "Governor",
      "officialIndices": [4]
    }
  ],
  "officials": [
    {
      "name": "Donald J. Trump"
    }, {
      "name": "Mike Pence"
    }, {
      "name": "Dianne Feinstein"
    }, {
      "name": "Kamala D. Harris"
    }, {
      "name": "Edmund G. Brown Jr.",
    }
  ]
}
我的最终目标如下:

[
  "officials": [
    {
      "name": "Donald J. Trump",
      "office": "President of the United States"
    }, {
      "name": "Mike Pence",
      "office": "Vice-President of the United States"
    }, {
      "name": "Dianne Feinstein",
      "office": "United States Senate"
    }, {
      "name": "Kamala D. Harris",
      "office": "United States Senate",
    }, {
      "name": "Edmund G. Brown Jr.",
      "office": "Governor"
    }
  ]
}
obj.officials.map(function(item, i){
  var j = 0;
  for (j in obj.offices)
  {
    if (obj.offices[j].officialIndices.includes(i)) {
        break;
    }
  }
  return {
    "name": item.name,
    "office": obj.offices[j].name
  }
})
所以这是我失败的尝试

var representatives = data;

function addOffice(name, indices){
  _.forEach(indices, function(value, key) {
    representatives.officials[key].office = name;
  });
}

_.forEach(representatives.offices, function(value, key) {
  // console.log("Office: '" + value.name + "' should be applied to officials " + value.officialIndices);
  addOffice(value.name, value.officialIndices);
});

// remove `offices` as it's now redundant
delete representatives.offices;

console.log(representatives);
使用此功能:

function addOffice(obj) {
  return {
    officials : obj.officials.map(function(official, idx){
      return {
        name : official.name,
        office : obj.offices.filter(function(office) {
          return office.officialIndices.indexOf(idx) > -1
        })[0].name
      }
    })
  }
}

您可以这样做:

[
  "officials": [
    {
      "name": "Donald J. Trump",
      "office": "President of the United States"
    }, {
      "name": "Mike Pence",
      "office": "Vice-President of the United States"
    }, {
      "name": "Dianne Feinstein",
      "office": "United States Senate"
    }, {
      "name": "Kamala D. Harris",
      "office": "United States Senate",
    }, {
      "name": "Edmund G. Brown Jr.",
      "office": "Governor"
    }
  ]
}
obj.officials.map(function(item, i){
  var j = 0;
  for (j in obj.offices)
  {
    if (obj.offices[j].officialIndices.includes(i)) {
        break;
    }
  }
  return {
    "name": item.name,
    "office": obj.offices[j].name
  }
})

这里有一个解决方案,用于将
官员
在办公室的
官方骰子
中使用的结果展平。最后,我们使用转换所有
官员
及其关联的
办公室
的结果值

var result = _.flatMap(data.offices, function(office) {
  return _(data.officials)
    .pick(office.officialIndices)
    .map(function(official) {
      return _(official)
        .pick('name')
        .set('office', office.name)
        .value();
    }).value();
});
var数据={
“办事处”:[{
“姓名”:“美国总统”,
“officialIndices”:[0]
}, {
“姓名”:“美国副总统”,
“药剂师”:[1]
}, {
“名称”:“美国参议院”,
“药剂师”:[2,3]
}, {
“名称”:“总督”,
“药剂师”:[4]
}],
“官员”:[{
“姓名”:“唐纳德·J·特朗普”
}, {
“姓名”:“迈克·彭斯”
}, {
“姓名”:“Dianne Feinstein”
}, {
“姓名”:“卡马拉·D·哈里斯”
}, {
“姓名”:“Edmund G.Brown Jr.”,
}]
};
var结果=uu.flatMap(data.offices,函数(office)){
返回(数据.官员)
.pick(office.officialIndices)
.map(功能(官方){
返回(官方)
.pick('名称')
.set('office',office.name)
.value();
}).value();
});
控制台日志(结果)
body>div{min height:100%;top:0;}