繁体   English   中英

从另一个对象数组向一个对象添加值

[英]Add values to an object from another array of objects

所以我有一个预定义设备的范围:

$scope.equipment = [
    {slot: 7, colspan: 2, rowspan: 1},
    {slot: 0, colspan: 2, rowspan: 2},
    {slot: 10, colspan: 2, rowspan: 1},
    {slot: 8, colspan: 1, rowspan: 1},
    {slot: 9, colspan: 1, rowspan: 1},
    {slot: 6, colspan: 2, rowspan: 1},
    {slot: 17, colspan: 1, rowspan: 1},
    {slot: 18, colspan: 1, rowspan: 1},
    {slot: 1, colspan: 2, rowspan: 4},
    {slot: 19, colspan: 2, rowspan: 1},
    {slot: 4, colspan: 2, rowspan: 4},
    {slot: 5, colspan: 2, rowspan: 4},
    {slot: 14, colspan: 1, rowspan: 1},
    {slot: 15, colspan: 1, rowspan: 1},
    {slot: 2, colspan: 2, rowspan: 2},
    {slot: 16, colspan: 1, rowspan: 1},
    {slot: 15, colspan: 1, rowspan: 1},
    {slot: 3, colspan: 2, rowspan: 2}
  ];

如您所见,每个插槽都有自己的colspan和rowpan。

比起我拥有的装备项目对象阵列:

    $scope.equipmentItems = [
     {
        "id": 282,
        "upgrade": 15,
        "bind": 1,
        "slot": 0,
        "name": "Archridium Headpiece (BL)"
    },
     {
        "id": 147,
        "upgrade": 15,
        "bind": 1,
        "slot": 1,
        "name": "Archridium Suit (BL)"
    },
     {
        "id": 192,
        "upgrade": 15,
        "bind": 1,
        "slot": 2,
        "name": "Archridium Hands (BL)"
    },
     {
        "id": 237,
        "upgrade": 15,
        "bind": 1,
        "slot": 3,
        "name": "Archridium Shoes (BL)"
    },
     {
        "id": 3706,
        "upgrade": 0,
        "bind": 0,
        "slot": 4
    },
     {
        "id": 3707,
        "upgrade": 0,
        "bind": 0,
        "slot": 5
    },
     {
        "id": 3622,
        "upgrade": 0,
        "bind": 0,
        "slot": 6
    },
     {
        "id": 408,
        "upgrade": 0,
        "bind": 0,
        "slot": 7,
        "name": "Amulet Of Pain +7",
        "description": "Protect you from enemy's attacks and to give more chance to do Critical Attacks. "
    },
     {
        "id": 3194,
        "upgrade": 0,
        "bind": 0,
        "slot": 8,
        "name": "Ring of Luck +3",
        "description": "Increases your Critical Hit Rate."
    },
     {
        "id": 3193,
        "upgrade": 0,
        "bind": 0,
        "slot": 9,
        "name": "Critical Ring +3",
        "description": "Increases your Critical Attack Damage."
    },
     {
        "id": 2371,
        "upgrade": 0,
        "bind": 1,
        "slot": 10,
        "name": "Astral Board Card - K Red Crystal Edition",
        "description": "A mysterious card that summons Astral Board, one of the valuable Honorable Age's legacies and the essence of Core Technology. - K Red Crystal Edition_$4#(Right click to use) "
    },
     {
        "id": 3607,
        "upgrade": 0,
        "bind": 0,
        "slot": 13
    },
     {
        "id": 3607,
        "upgrade": 0,
        "bind": 0,
        "slot": 14
    },
    : {
        "id": 3604,
        "upgrade": 0,
        "bind": 0,
        "slot": 15
    },
     {
        "id": 3604,
        "upgrade": 0,
        "bind": 0,
        "slot": 16
    },
     {
        "id": 2568,
        "upgrade": 0,
        "bind": 4,
        "slot": 17,
        "name": "Leth Tyrant's Ring",
        "description": "This ring contains the sealed power of Leth Tyrant, the extraordinary monster created by the Doctor. "
    },
     {
        "id": 3184,
        "upgrade": 0,
        "bind": 4,
        "slot": 18,
        "name": "Killian's Ring",
        "description": "This generates powerful energy from the combination of Killian's Dark Energy and the grudge of the Black Cat, the Guardian of Hell."
    },
    "{
        "id": 2228,
        "upgrade": 0,
        "bind": 0,
        "slot": 19,
        "name": "Belt of Damp +4",
        "description": "This belt reduces damage from intensified sword or magic attacks. "
    }
];

如您所见,每个对象键基本上都是插槽ID。

比我使用这段代码

      $scope.equipmentItems = _.sortBy($scope.equipmentItems, function(obj) {
    return _.indexOf([7, 0, 10, 8, 9, 6, 17, 18, 1, 19, 4, 5, 14, 13, 2, 16, 15, 3], obj.slot);
  });

将设备项密钥排序到我的特定订单。

无论如何,我想要的是最高性能的代码片段,可以将$ scope.equipment的值colspan和rowspan添加到$ scope.equipmentItems $ scope.equipment插槽对应于$ scope.equipmentItems插槽。

您可以使用Array函数。 像这样:

let merged = equipmentItems.map(item=>{
  item.anotherKey = equipment.find(it=> it.slot == item.slot);
  return item;
});

如果您不喜欢这样,或者您无法使用这些方法,则可以使用forEach

equipmentItems.forEach(function(item){
    equipment.forEach(function(item_){
      if(item.slot == item_.slot){
        item.someKey = item_;
      }
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM