簡體   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