I have an array of objects and want to create all possible unique combinations based on two keys.
实例 -
[
{ 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 } ]
},
]
现在,我很想知道,如何产生所有变化,以创造预期产出,特别是认识到这样做需要扩大规模,使其达到更大的规模。 我知道答案是再入侵,但对于我的生活来说,我sil的头脑可以 figure灭。 感谢任何帮助!