获取相对于组的所有uniq组合



我有一组属性值:

示例:

[
  {
    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);
    }
  });

这在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 repled 中看到此操作。

最新更新