我有一组属性值:
示例:
[
{
memberAttribute: { attributeName: 'a' },
value: '1'
},
{
memberAttribute: { attributeName: 'a' },
value: '2'
},
{
memberAttribute: { attributeName: 'b' },
value: '1'
},
{
memberAttribute: { attributeName: 'b' },
value: '2'
}
]
现在我想获得给定成员属性的所有独特组合。
因此,如果我想要成员属性的独特组合' a'和' b'结果将是:
[
{
memberAttribute: { attributeName: 'a' },
value: '1'
},
{
memberAttribute: { attributeName: 'b' },
value: '1'
}
],
[
{
memberAttribute: { attributeName: 'a' },
value: '1'
},
{
memberAttribute: { attributeName: 'b' },
value: '2'
}
],
[
{
memberAttribute: { attributeName: 'a' },
value: '2'
},
{
memberAttribute: { attributeName: 'b' },
value: '1'
}
],
[
{
memberAttribute: { attributeName: 'a' },
value: '2'
},
{
memberAttribute: { attributeName: 'b' },
value: '2'
}
]
我需要能够提供n个输入成员属性,但只能获得2个输入属性的预期结果。
当前可怕的解决方案:
export const getAttributeCombinations = (
attributes: MemberAttributeValue[]
) => {
// TODO - This algorithm only supports 2 attribute types
// It should support any number of attribute types
const combinations = new Array<Array<MemberAttributeValue>>();
for (const attribute of attributes) {
let unusedAttributes = allExcept(attribute, attributes);
const permutate = () => {
const combination = [attribute];
const toRemove = new Array<Number>();
for (let i = 0; i < unusedAttributes.length; i++) {
const unusedAttribute = unusedAttributes[i];
if (!attributeTypeAlreadyExists(unusedAttribute, combination)) {
toRemove.push(i);
combination.push(unusedAttribute);
}
}
for (const index of toRemove) {
unusedAttributes = remove(index, 1, unusedAttributes);
}
combinations.push(combination);
};
permutate();
while (unusedAttributes.length > 0) {
permutate();
}
}
const sortedCombinations = map(sortByAttributeName, combinations);
return uniqByCombination(sortedCombinations);
};
失败的笑话测试示例:
it('given 3 attribute types should return 12 combinations', () => {
const inclusionAttributes: MemberAttributeValue[] = [
{
memberAttribute: {
attributeName: 'gender',
aliases: [],
contentType: ContentType.String,
type: AttributeType.Mandatory
},
value: 'Male'
},
{
memberAttribute: {
attributeName: 'gender',
aliases: [],
contentType: ContentType.String,
type: AttributeType.Mandatory
},
value: 'Female'
},
{
memberAttribute: {
attributeName: 'age band',
aliases: [],
contentType: ContentType.String,
type: AttributeType.Mandatory
},
value: '0-50'
},
{
memberAttribute: {
attributeName: 'age band',
aliases: [],
contentType: ContentType.String,
type: AttributeType.Mandatory
},
value: '51+'
},
{
memberAttribute: {
attributeName: 'likes',
aliases: [],
contentType: ContentType.String,
type: AttributeType.Mandatory
},
value: 'cats'
},
{
memberAttribute: {
attributeName: 'likes',
aliases: [],
contentType: ContentType.String,
type: AttributeType.Mandatory
},
value: 'dogs'
},
{
memberAttribute: {
attributeName: 'likes',
aliases: [],
contentType: ContentType.String,
type: AttributeType.Mandatory
},
value: 'goats'
}
];
const combinations = getAttributeCombinations(inclusionAttributes);
expect(combinations.length).toBe(12);
for (const combination of combinations) {
expect(combination.length).toBe(3);
}
});
答案 0 :(得分:3)
这在Ramda中相对简单,只是Ramda的xprod
函数仅适用于两个列表。如果它在列表列表上工作,我们可以在几个步骤中完成此操作。但编写我们自己的文章很容易:
const xproduct = reduce(pipe(xprod, map(unnest)), [[]])
const transform = pipe(
groupBy(path(['memberAttribute', 'attributeName'])),
values,
xproduct
)
const inclusionAttributes = [
{"memberAttribute": {"attributeName": "gender"}, "value": "Male"},
{"memberAttribute": {"attributeName": "gender"}, "value": "Female"},
{"memberAttribute": {"attributeName": "age band"}, "value": "0-50"},
{"memberAttribute": {"attributeName": "age band"}, "value": "51+"},
{"memberAttribute": {"attributeName": "likes"}, "value": "cats"},
{"memberAttribute": {"attributeName": "likes"}, "value": "dogs"},
{"memberAttribute": {"attributeName": "likes"}, "value": "goats"}
]
console.log(transform(inclusionAttributes))
//=> Male/0-50/cats, Male/0-50/dogs, Male/0-50/goats, Male/51+/cats,...
您可以在 Ramda REPL 上看到这一点。