所有可能的组合与JSON文件中的数据 [英] All possible combinations with data from JSON file

查看:55
本文介绍了所有可能的组合与JSON文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal is to create a part of code that would generate all possible combinations without duplicates (combinations with same elements, no matter what their sequence is) with data from a JSON file. My JSON file looks like this:

[
 {
   "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"
 }, ...
]

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

Combinations would be generated in the following way:

[
    "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个元素,并且根据皮肤"属性值):

Note that in this case both combinations have the same elements and therefore shouldn't be noted twice. That means that as long as there are elements in a combination that are same to another possible combination (no matter what sequence they are in) it counts as one combination (every combination will have 10 elements, and would be distinct from another based on the "SKIN" attribute values):

[
    "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

Also note that the same item can appear in a combination multiple times (up to 10) and that I'm workig with a JSON file of around 1500 elements, so efficiency is key. To sum it up the final product should look something like this: https://textuploader.com/1du6o

这也是一个类似的问题,但不太复杂: JavaScript中的排列?

This is also a kinda similar problem, but less complex: Permutations in JavaScript?

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

I've tried to sort this out with bubble sort and such, but haven't succeeded thus far. If you have any ideas how to make this happen I'd love to hear them.

推荐答案

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

Although it's not entirely clear how to determine combinations the way you suggest, this might get you part of the way there.

创建一个数据结构来存储 collection 武器皮肤等级的唯一元素:

Create a data structure to store unique elements for collection, weapon, skin, and grade:

// 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' } }

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

Generate all combinations from the map's sets by iterating using nested loops:

// 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

这篇关于所有可能的组合与JSON文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆