我想从可变长度的多维数组中创建数据集,但我不确定如何最好地解决它。
我想过计算数组的排列然后循环遍历值来创建新的数据集,但这种方法看起来很脆弱。
以下是示例输入和输出。
输入
[ [id: "Denver", value: ['$', '%']], [id: "datestamp", value: [201106]], [id: "price", value: [1, 2]] ]
预期输出
[ ["Denver$2011061"], ["Denver%2011061"], ["Denver$2011062"], ["Denver%2011062"] ]
答案 0 :(得分:2)
假设在Groovy中,你有:
def data = [ [id: "Denver", value: ['$', '%']], [id: "datestamp", value: [201106]], [id: "price", value: [1, 2]] ]
然后你可以这样做:
def output = data*.value.combinations()*.join('').collect { [ "${data[0].id}$it" ] }
输出将具有值:
[[Denver$2011061], [Denver%2011061], [Denver$2011062], [Denver%2011062]]