如何基于变化的索引合并两个对象

时间:2017-02-20 06:33:27

标签: javascript object lodash

我在从API端点返回的数组中有两个对象,它根据语言环境公开选举产生的官员。一个是offices,另一个是officials。我需要了解如何将这两个对象合并为一个更有意义的对象。

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

所以这是我失败的尝试。

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);

4 个答案:

答案 0 :(得分:1)

var offices = dataObj['offices'];
var officials = dataObj['officials'];
var mergedArray = {officials : []};

offices.forEach(function(obj, index) {
    var indices = obj['officialIndices'];
    indices.forEach(function(indice, ind) {
        var newObj = {};
        newObj['name'] = officials[indice]['name'];
        newObj['office'] = obj['name'];
        mergedArray['officials'].push(newObj);
    })
})

// mergedArray is your answer

答案 1 :(得分:0)

使用此功能:

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

答案 2 :(得分:0)

你可以这样做:

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

答案 3 :(得分:0)

这是一个使用flatMap来平衡officials在办公室officialIndices中使用pick的结果的解决方案。最后,我们使用map将所有officials的结果值与其关联的office进行转换。

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 data = {
  "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.",
  }]
};

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();
});

console.log(result);

body > div { min-height: 100%; top: 0; }

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
&#13;
&#13;
&#13;