So i have this scope with a predefined equipment:
$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}
];
As you can see, each slot has its own colspan and rowspan.
Than i have the equipment items array of objects:
$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. "
}
];
As you can see each object key is basically the slot id.
Than i use this code
$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);
});
to sort the equipment items keys to my specific order.
Anyway, what i want is the most performant piece of code that can add the values colspan and rowspan from $scope.equipment to $scope.equipmentItems WHERE $scope.equipment slot correspond to $scope.equipmentItems slot.
You can use Array
functions. Something like this:
let merged = equipmentItems.map(item=>{
item.anotherKey = equipment.find(it=> it.slot == item.slot);
return item;
});
If you don't like this, or you can't use those methods you can use forEach
equipmentItems.forEach(function(item){
equipment.forEach(function(item_){
if(item.slot == item_.slot){
item.someKey = item_;
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.