所有可能的组合与JSON文件中的数据

时间:2019-06-17 14:55:03

标签: node.js json combinations combinatorics

我的目标是创建一部分代码,该代码将使用JSON文件中的数据生成所有可能的组合,而不会重复(具有相同元素的组合,无论其顺序如何)。我的JSON文件如下所示:

[
 {
   "COLLECTION": "Assault",
   "WEAPON": "SG 553",
   "SKIN": "Tornado",
   "GRADE": "Consumer Grade"
 },
 {
   "COLLECTION": "Assault",
   "WEAPON": "UMP-45",
   "SKIN": "Caramel",
   "GRADE": "Consumer Grade"
 },
 {
   "COLLECTION": "Vertigo",
   "WEAPON": "Five-SeveN",
   "SKIN": "Candy Apple ",
   "GRADE": "Industrial Grade"
 }, ...
]

组合将通过以下方式生成:

[
    "COMBINATION 1":[
     {
       "COLLECTION": "Assault",
       "WEAPON": "SG 553",
       "SKIN": "Tornado",
       "GRADE": "Consumer Grade"
     },
     {
       "COLLECTION": "Assault",
       "WEAPON": "UMP-45",
       "SKIN": "Caramel",
       "GRADE": "Consumer Grade"
     },
     {
       "COLLECTION": "Assault",
       "WEAPON": "Five-SeveN",
       "SKIN": "Candy Apple ",
       "GRADE": "Industrial Grade"
     }, ...
    ],

    "COMBINATION 2":[
     {
       "COLLECTION": "Assault",
       "WEAPON": "SG 553",
       "SKIN": "Tornado",
       "GRADE": "Consumer Grade"
     },
    {
       "COLLECTION": "Aztec",
       "WEAPON": "M4A4",
       "SKIN": "Jungle Tiger",
       "GRADE": "Industrial Grade"
     },
     {
       "COLLECTION": "Aztec",
       "WEAPON": "Tec-9",
       "SKIN": "Ossified",
       "GRADE": "Mil-Spec"
     }, ...
    ],...
]

请注意,在这种情况下,两种组合都具有相同的元素,因此不应重复两次。这意味着只要组合中的某些元素与另一个可能的组合相同(无论它们处于什么顺序),就算作一个组合(每个组合将包含10个元素,并且根据“皮肤”属性值):

[
    "COMBINATION 1":[
     {
       "COLLECTION": "Vertigo",
       "WEAPON": "SG 553",
       "SKIN": "Tornado",
       "GRADE": "Consumer Grade"
     },
     {
       "COLLECTION": "Assault",
       "WEAPON": "UMP-45",
       "SKIN": "Caramel",
       "GRADE": "Consumer Grade"
     },
     {
       "COLLECTION": "Assault",
       "WEAPON": "Five-SeveN",
       "SKIN": "Candy Apple ",
       "GRADE": "Industrial Grade"
     },...
    ],

    "COMBINATION 2":[
    {
       "COLLECTION": "Assault",
       "WEAPON": "Five-SeveN",
       "SKIN": "Candy Apple ",
       "GRADE": "Industrial Grade"
     },
      {
       "COLLECTION": "Vertigo",
       "WEAPON": "SG 553",
       "SKIN": "Tornado",
       "GRADE": "Consumer Grade"
     },
     {
       "COLLECTION": "Assault",
       "WEAPON": "UMP-45",
       "SKIN": "Caramel",
       "GRADE": "Consumer Grade"
     },...
],...

还请注意,同一项目可以多次出现(最多10次),并且我正在处理包含1500个元素的JSON文件,因此效率是关键。总结起来,最终产品应该看起来像这样: https://textuploader.com/1du6o

这也是一个类似的问题,但不太复杂: Permutations in JavaScript?

我试图用冒泡排序之类的方法来解决这个问题,但是到目前为止还没有成功。如果您有任何想法来实现这一目标,我很想听听他们的想法。

1 个答案:

答案 0 :(得分:1)

尽管尚不清楚如何以您的建议方式确定组合,但这可能会让您有所作为。

创建一个数据结构来存储collectionweaponskingrade的唯一元素:

// JSON sources.
const sources = [
  {
    "COLLECTION": "Assault",
    "WEAPON": "SG 553",
    "SKIN": "Tornado",
    "GRADE": "Consumer Grade"
  },
  {
    "COLLECTION": "Assault",
    "WEAPON": "UMP-45",
    "SKIN": "Caramel",
    "GRADE": "Consumer Grade"
  },
  {
    "COLLECTION": "Vertigo",
    "WEAPON": "Five-SeveN",
    "SKIN": "Candy Apple ",
    "GRADE": "Industrial Grade"
  },
]

// Create storage for the sources. Use sets to prevent duplicates.
const map = new Map([
  ['COLLECTION', new Set()],
  ['WEAPON', new Set()],
  ['SKIN', new Set()],
  ['GRADE', new Set()]
])

// Load each source into the map.
sources.forEach((source) => {
  Object.keys(source).forEach((key) => {
    const set = map.get(key)
    set.add(source[key])
  })
})

console.log(map)

输出:

Map {
  'COLLECTION' => Set { 'Assault', 'Vertigo' },
  'WEAPON' => Set { 'SG 553', 'UMP-45', 'Five-SeveN' },
  'SKIN' => Set { 'Tornado', 'Caramel', 'Candy Apple ' },
  'GRADE' => Set { 'Consumer Grade', 'Industrial Grade' } }

通过使用嵌套循环进行迭代,从地图的集合中生成所有组合:

// Store the generated combinations.
const combinations = []

// Generate all combinations.
for (const collection of map.get('COLLECTION')) {
  for (const weapon of map.get('WEAPON')) {
    for (const skin of map.get('SKIN')) {
      for (const grade of map.get('GRADE')) {
        combinations.push({
          'COLLECTION': collection,
          'WEAPON': weapon,
          'SKIN': skin,
          'GRADE': grade,
        })
      }
    }
  }
}

console.log(combinations.length) // 36