在 Lodash 中用其他两个 JSON 数组的所有组合标记数据项
Label data-items with all combinations of two other JSON arrays in Lodash
我正在使用 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" ]]
与 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
这很好用:(其中 x = 来源 json)
第 1 步。获取区域和轴的点积
DOT = _(x.regimes)
.map((r) => {
return x.axes.map((a)=> {
return {
regime: r,
axis: a
}
})
})
.flatten()
第 2 步。获取数据值
data = _(x.data)
.map((v) => {
return {data:v};
}
)
.value()
步骤 3. 合并两者
// Merge the two collections
result = _(DOT).merge(data)
任务完成。
我正在使用 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" ]]
与 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
这很好用:(其中 x = 来源 json)
第 1 步。获取区域和轴的点积
DOT = _(x.regimes)
.map((r) => {
return x.axes.map((a)=> {
return {
regime: r,
axis: a
}
})
})
.flatten()
第 2 步。获取数据值
data = _(x.data)
.map((v) => {
return {data:v};
}
)
.value()
步骤 3. 合并两者
// Merge the two collections
result = _(DOT).merge(data)
任务完成。