【问题标题】:Label data-items with all combinations of two other JSON arrays in Lodash在 Lodash 中使用其他两个 JSON 数组的所有组合标记数据项
【发布时间】:2017-05-18 10:33:11
【问题描述】:

我正在使用 Lodash 将一种 JSON 结构转换为另一种。我需要形成两个数组的所有组合,并将每个组合与取自第三个数组的数据值结合起来。

如何生成所有组合并为每个组合提取正确的数据项?

来源 JSON 结构:

{
    "regimes"      :   [ "AA", "BB" ],
    "axes"         :   [ "Horizontal", "Vertical" ],
    "data-items"   :   [ 1, 2, 3, 4 ]
}


目标 JSON 结构:

[
    {
        "regime"      : "AA",
        "axis"        : "Horizontal",
        "data-item"   : 1
    },
    {
        "regime"      : "AA",
        "axis"        : "Vertical",
        "data-item"   : 2
    },
    {
        "regime"      : "BB",
        "axis"        : "Horizontal",
        "data-item"   : 3
    },
    {
        "regime"      : "BB",
        "axis"        : "Vertical",
        "data-item"   : 4
    }
] 

【问题讨论】:

  • 你不需要{"regime" : "AA", "axis" : "Horizontal", "data-item" : 3}和4吗?
  • 没有。我不是在寻找所有三个数组的所有组合。相反,前两个的所有组合,然后将每个组合与从第三个数组中按顺序获取的数据项相关联。这有点反直觉! - 我已经仔细检查了上面的 JSON,它完全符合我的需要。

标签: javascript json lodash map-function


【解决方案1】:

我想出了那种混合而丑陋的解决方案。也许你可以重构它。

我要做的是,

1) 拿cartesian product ofa["regimes"]a["axes"] 给你,

[ "AA", "Horizontal" ] 
[ "AA", "Vertical" ] 
[ "BB", "Horizontal" ] 
[ "BB", "Vertical" ]

2) 然后,将a["data-items"] 切片为

first = [1,2]
last = [3,4]

3) 和zip

[[ "AA", "Horizontal" ],[ "AA", "Vertical" ]]

first = [1,2]

[[ "BB", "Horizontal" ],[ "BB", "Vertical" ]]

last。这会给我们

[[ "AA", "Horizontal" ], 1]
[[ "AA", "Vertical" ],2]
[[ "BB", "Horizontal" ],3]
[[ "BB", "Vertical" ],4]

最后我们可以在上面的数组数组上轻松调用map

above_multidimensional_array.map(e=>_.flatten(e))


_.zip(cartesianProductOf(a.regimes, a.axes)
  .slice(0,2), a["data-items"]
  .slice(0,2))
  .map(e=>_.flatten(e)
).concat(
  _.zip(cartesianProductOf(a.regimes, a.axes)
    .slice(2,4), a["data-items"]
    .slice(2,4))
    .map(e=>_.flatten(e))
).map(e=>({'regime': e[0], 'axis': e[1], 'data-item': e[2]}))

结果:

[
    { regime: "AA", axis: "Horizontal", data-item: 1 } 
    { regime: "AA", axis: "Vertical", data-item: 2 } 
    { regime: "BB", axis: "Horizontal", data-item: 3 } 
    { regime: "BB", axis: "Vertical", data-item: 4 }
]

笛卡尔积实现。

#https://gist.github.com/ijy/6094414#file-cartesian-product-js
function cartesianProductOf() {
    return _.reduce(arguments, function(a, b) {
        return _.flatten(_.map(a, function(x) {
            return _.map(b, function(y) {
                return x.concat([y]);
            });
        }), true);
    }, [ [] ]);
};

#https://gist.github.com/FestivalBobcats/1323387
function cartesianProductOf(){
    return _.reduce(arguments, function(mtrx, vals){
        return _.reduce(vals, function(array, val){
            return array.concat(
                _.map(mtrx, function(row){ return row.concat(val); })
            );
        }, []);
    }, [[]]);
}

Cartesian product of multiple arrays in JavaScript

【讨论】:

  • 对不起@marmeladze 我想你已经使用了我需要的输出作为你的输入!我的输入是第一个块,我需要的输出是第二个块。也许?
  • 该死的,我是怎么做到的?让我修复它-)))
【解决方案2】:

这很好用:(其中 x = 源 json)

第 1 步。获取制度和轴的 DOT 积

    DOT = _(x.regimes)
        .map((r)  => {
            return x.axes.map((a)=> {
                return {
                    regime: r,
                    axis: a
                }
            })
        })
        .flatten()


Step 2. 获取数据值

    data = _(x.data)
        .map((v)  => {
            return {data:v};
            }
        )
        .value()


步骤 3. 合并两者

    // Merge the two collections

    result = _(DOT).merge(data)

工作完成。

【讨论】:

    猜你喜欢
    • 2017-12-10
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2018-07-04
    相关资源
    最近更新 更多