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

时间:2016-06-19 19:54:26

标签: javascript arrays angularjs json object

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

$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和rowspan。

我拥有设备项目对象数组:

    $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 WHERE $ scope的最高性能代码片段.equipment slot对应于$ scope.equipmentItems槽。

1 个答案:

答案 0 :(得分:0)

您可以使用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_;
      }
  });
});