如何在 JavaScript 中根据概率组合对象数组?

Posted

技术标签:

【中文标题】如何在 JavaScript 中根据概率组合对象数组?【英文标题】:How to combine arrays of objects according probability in JavaScript? 【发布时间】:2020-07-27 18:00:32 【问题描述】:

我有一个包含 3 个对象数组的数组。

[
  [
    
      VKORG: "3060",
      VTWEG: "AS",
      VRKME: "CX",
      LVORM: ""
    ,
    
      VKORG: "3060",
      VTWEG: "MF",
      VRKME: "CX",
      LVORM: ""
    
  ],
  [
    
      MEINH: "CX",
      UMREZ: "12",
      UMREN: "1"
    
  ],
  [
    
      WERKS: "3060",
      LGORT: "0011"
    ,
    
      WERKS: "3060",
      LGORT: "HU00"
    
  ]
]

所以我想组合其中每个数组的对象,根据它们之间的组合概率......所以它应该是这样的:

[
    
      VKORG: "3060",
      VTWEG: "AS",
      VRKME: "CX",
      LVORM: "",
      MEINH: "CX",
      UMREZ: "12 ",
      UMREN: "1 ",
      WERKS: "3060",
      LGORT: "0011"
    ,
    
      VKORG: "3060",
      VTWEG: "MF",
      VRKME: "CX",
      LVORM: "",
      MEINH: "CX",
      UMREZ: "12 ",
      UMREN: "1 ",
      WERKS: "3060",
      LGORT: "0011"
    ,
    
      VKORG: "3060",
      VTWEG: "AS",
      VRKME: "CX",
      LVORM: "",
      MEINH: "CX",
      UMREZ: "12 ",
      UMREN: "1 ",
      WERKS: "3060",
      LGORT: "HU00"
    ,
    
      VKORG: "3060",
      VTWEG: "MF",
      VRKME: "CX",
      LVORM: "",
      MEINH: "CX",
      UMREZ: "12 ",
      UMREN: "1 ",
      WERKS: "3060",
      LGORT: "HU00"
    
  ]
;

如何使用 javascript 做到这一点?我花了很多时间来用 lodash 来做这件事,但直到这里什么都没有。

【问题讨论】:

组合概率”是什么意思,如何计算? 我的错...我的意思是必须根据可能的概率组合对象,例如:[ ...array1[0], ...array2[0], ...array3 [0] , ...array1[0], ...array2[0], ...array3[1] , ...array1[1], ...array2[0], ... .array3[0] , ...array1[1], ...array2[0], ...array3[1] ] 您是指所有“可能的”组合吗? 【参考方案1】:

您可以使用Array.flatMap()Array.map() 创建一个组合数组的递归函数:

const cartesian = ([a, ...arrs]) => 
  arrs.length ? // if there are more arrays
    a.flatMap(o1 => cartesian(arrs) // run them through cartesian, and combine with current object
      .map(o2 => (
        ...o1,
        ...o2
      ))
    ) : a // return the current array

const arrs = [[ VKORG: "3060", VTWEG: "AS", VRKME: "CX", LVORM: "" ,  VKORG: "3060", VTWEG: "MF", VRKME: "CX", LVORM: "" ], [ MEINH: "CX", UMREZ: "12", UMREN: "1" ], [ WERKS: "3060", LGORT: "0011" ,  WERKS: "3060", LGORT: "HU00" ]]

const results = cartesian(arrs)

console.log(results)

【讨论】:

【参考方案2】:

您可以对笛卡尔积采用标准算法并获得连接对象作为结果。

var data = [[ VKORG: "3060", VTWEG: "AS", VRKME: "CX", LVORM: "" ,  VKORG: "3060", VTWEG: "MF", VRKME: "CX", LVORM: "" ], [ MEINH: "CX", UMREZ: "12", UMREN: "1" ], [ WERKS: "3060", LGORT: "0011" ,  WERKS: "3060", LGORT: "HU00" ]],
    result = data
        .reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []))
        .map(a => Object.assign(, ...a));

console.log(result);
.as-console-wrapper  max-height: 100% !important; top: 0; 

【讨论】:

以上是关于如何在 JavaScript 中根据概率组合对象数组?的主要内容,如果未能解决你的问题,请参考以下文章

概率论里面这个括号里面上m下n是啥啊(里面有图)

如何直观地理解排列数和组合数

根据先前数据的概率生成数据组合

如何根据 PyTorch 中的概率计算交叉熵?

概率论基础知识(Probability Theory)

Gym10081 A - Arcade Game -康托展开全排列组合数变成递推的思想