javascript - 使用 Lodash 中其他两个 JSON 数组的所有组合标记数据项

标签 javascript json lodash map-function

我正在使用 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
    }
] 

最佳答案

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

我要做的是,

1) 取cartesian product of , a["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" ]]

最后。这会给我们

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

关于javascript - 使用 Lodash 中其他两个 JSON 数组的所有组合标记数据项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44045365/

相关文章:

javascript - sails js 上的自定义和多语言消息错误如何解决

javascript - 将类添加到 Vuetify 的 v-select 组件的第一项

javascript - 对象数组的 Angular JSON 过滤器

javascript - json_encode 和 JSON.parse 不协调

javascript - 按对象中的帖子 ID 对评论进行分组

javascript - 传播 Promise 并在新对象中扩展

php - 未捕获的语法错误 : Unexpected token ILLEGAL

javascript - 通过选中复选框添加或删除 PHP 变量

javascript - 模型不更新所选值 - AngularJS

javascript - 在javascript中订购带有多个点的字符串