我有一个对象数组,想要基于两个键创建所有可能的唯一组合。
输入示例 -
[
{ slot: 'body', spell: 'combat.i', item: 'body combat1'},
{ slot: 'body', spell: 'combat.i', item: 'body combat2'},
{ slot: 'body', spell: 'strength.i', item: 'body str1' },
{ slot: 'body', spell: 'dexterity.i', item: 'body dex1' },
{ slot: 'legs', spell: 'dexterity.i', item: 'legs dex1' },
{ slot: 'legs', spell: 'combat.i', item: 'legs combat1' },
{ slot: 'legs', spell: 'strength.i', item: 'legs str1' },
{ slot: 'head', spell: 'dexterity.i', item: 'head dex1' },
{ slot: 'head', spell: 'combat.i', item: 'head combat1' },
{ slot: 'head', spell: 'strength.i', item: 'head str1' },
]
理想的输出是这样的
[
[
{ slot: 'body', spell: 'combat.i', item: 'body combat1' },
{ slot: 'legs', spell: 'dexterity.i', item: 'legs dex1' },
{ slot: 'head', spell: 'strength.i', item: 'head str1' },
],
[
{ slot: 'body', spell: 'combat.i', item: 'body combat2' },
{ slot: 'legs', spell: 'dexterity.i', item: 'legs dex1' },
{ slot: 'head', spell: 'strength.i', item: 'head str1' },
],
[
{ slot: 'body', spell: 'strength.i', item: 'body str' },
{ slot: 'legs', spell: 'dexterity.i', item: 'legs dex1' },
{ slot: 'head', spell: 'combat.i', item: 'head combat1' },
],
...etc
]
这样最终的产品将是每个插槽/法术/物品的所有组合,没有任何重复(不考虑顺序)。
我的第一个想法是将数据组织到每个插槽的一个对象中,并在其中为每个效果组织一个数组。
const generateList = (data, slots, effects) => {
const matches = {};
slots.forEach(slot => {
matches[slot] = {};
effects.forEach(effect => {
matches[slot][effect] = data.filter(item => item.slot === slot && item.spell === effect);
})
});
return matches
};
生成
{
body: {
'combat.i': [
{ slot: 'body', spell: 'combat.i', item: 'body combat1' },
{ slot: 'body', spell: 'combat.i', item: 'body combat2' }
],
'strength.i': [ { slot: 'body', spell: 'strength.i', item: 'body str1' } ],
'dexterity.i': [ { slot: 'body', spell: 'dexterity.i', item: 'body dex1' } ]
},
legs: {
'combat.i': [ { slot: 'legs', spell: 'combat.i', item: 'legs combat1' } ],
'strength.i': [ { slot: 'legs', spell: 'strength.i', item: 'legs str1' } ],
'dexterity.i': [ { slot: 'legs', spell: 'dexterity.i', item: 'legs dex1' } ]
},
head: {
'combat.i': [ { slot: 'head', spell: 'combat.i', item: 'head combat1' } ],
'strength.i': [ { slot: 'head', spell: 'strength.i', item: 'head str1' } ],
'dexterity.i': [ { slot: 'head', spell: 'dexterity.i', item: 'head dex1' } ]
},
]
我现在有点困惑如何生成所有排列以创建所需的输出,特别是知道这需要扩展到更大的数字。我知道答案与递归有关,但就我的一生而言,我愚蠢的前端大脑无法弄清楚。任何帮助将不胜感激!
您需要首先按“槽”组织数据,然后在每个槽内按“拼写”组织数据,这是一个好的开始。整理数据后,就可以生成组合了。
# Example input
items = [
{'slot': 'body', 'spell': 'combat.i', 'item': 'body combat1'},
{'slot': 'body', 'spell': 'combat.i', 'item': 'body combat2'},
{'slot': 'body', 'spell': 'strength.i', 'item': 'body str1'},
{'slot': 'body', 'spell': 'dexterity.i', 'item': 'body dex1'},
{'slot': 'legs', 'spell': 'dexterity.i', 'item': 'legs dex1'},
{'slot': 'legs', 'spell': 'combat.i', 'item': 'legs combat1'},
{'slot': 'legs', 'spell': 'strength.i', 'item': 'legs str1'},
{'slot': 'head', 'spell': 'dexterity.i', 'item': 'head dex1'},
{'slot': 'head', 'spell': 'combat.i', 'item': 'head combat1'},
{'slot': 'head', 'spell': 'strength.i', 'item': 'head str1'},
]
# Step 1: Organize the data
organized = {}
for item in items:
slot = item['slot']
spell = item['spell']
if slot not in organized:
organized[slot] = {}
if spell not in organized[slot]:
organized[slot][spell] = []
organized[slot][spell].append(item)
# Step 2: Generate combinations
# Convert each slot's spells into a list of items
slot_combinations = [list(spell.values()) for spell in organized.values()]
# Generate all combinations using product
all_combinations = list(product(*[item for sublist in slot_combinations for item in sublist]))
# Convert from tuple back to list format for the final output
final_combinations = [[item for item in combo] for combo in all_combinations]
# Example to print the first 5 combinations for brevity
for combo in final_combinations[:5]:
print(combo)