我有一个json对象数组,我需要按四个数组将它们组合在一起以创建单个对象。最终,我的目标是在SQL Sever中使用OPENJSON将值插入表中,每个对象都是一行。
似乎应该足够简单。我已经在网上搜索了很多东西,并且遇到了_.groupBy(),$。extend(true,{},x,y)等各种事物。等等,但是似乎都没有我想要的。
我有以下JSON对象数组:
[{"name":"locMLR","value":"6485183463415278686"},
{"name":"dayDate","value":"04/29/2019"},{"name":"intStps","value":"1"},
{"name":"intPkgs","value":"2"},
{"name":"locMLR","value":"6485183463415278686"},
{"name":"dayDate","value":"04/30/2019"},{"name":"intStps","value":"1"},
{"name":"intPkgs","value":"2"},
{"name":"locMLR","value":"6485183463415278686"},
{"name":"dayDate","value":"05/01/2019"},{"name":"intStps","value":"1"},
{"name":"intPkgs","value":"2"},
{"name":"locMLR","value":"6485183463415278686"},
{"name":"dayDate","value":"05/02/2019"},{"name":"intStps","value":"1"},
{"name":"intPkgs","value":"2"},
{"name":"locMLR","value":"6485183463415278686"},
{"name":"dayDate","value":"05/03/2019"},{"name":"intStps","value":"1"},
{"name":"intPkgs","value":"2"}]
我需要这样分组:
Object {
{"name":"locMLR","value":"6485183463415278686"},
{"name":"dayDate","value":"04/29/2019"},
{"name":"intStps","value":"1"},
{"name":"intPkgs","value":"2"}
},
Object {
{"name":"locMLR","value":"6485183463415278686"},
{"name":"dayDate","value":"04/30/2019"},
{"name":"intStps","value":"1"},
{"name":"intPkgs","value":"2"}
},
Object {
{"name":"locMLR","value":"6485183463415278686"},
{"name":"dayDate","value":"05/01/2019"},{"name":"intStps","value":"1"},
{"name":"intPkgs","value":"2"}
}
答案 0 :(得分:1)
您可能想要一个类似chunk
的函数,该函数需要块大小和一个数组,并将元素分组为该大小的块:
const chunk = (n, arr, res = []) => arr.length == 0
? res
: chunk (n, arr.slice(n), res.concat([arr.slice(0, n)]))
const arr = [{"name": "locMLR", "value": "6485183463415278686"}, {"name": "dayDate", "value": "04/29/2019"}, {"name": "intStps", "value": "1"}, {"name": "intPkgs", "value": "2"}, {"name": "locMLR", "value": "6485183463415278686"}, {"name": "dayDate", "value": "04/30/2019"}, {"name": "intStps", "value": "1"}, {"name": "intPkgs", "value": "2"}, {"name": "locMLR", "value": "6485183463415278686"}, {"name": "dayDate", "value": "05/01/2019"}, {"name": "intStps", "value": "1"}, {"name": "intPkgs", "value": "2"}, {"name": "locMLR", "value": "6485183463415278686"}, {"name": "dayDate", "value": "05/02/2019"}, {"name": "intStps", "value": "1"}, {"name": "intPkgs", "value": "2"}, {"name": "locMLR", "value": "6485183463415278686"}, {"name": "dayDate", "value": "05/03/2019"}, {"name": "intStps", "value": "1"}, {"name": "intPkgs", "value": "2"}]
console.log(chunk(4, arr))
有许多替代方法可以编写此代码。但是,这种递归解决方案简单明了,并且当您的JS引擎执行尾部调用优化时,它应该相对有效。
如果要执行此操作而无需递归调用,可以使用reduce
折叠数组,如下所示:
const chunk = (n, xs) => xs.reduce(
(a, x, i) => i % n == 0
? a.concat([[x]])
: a.slice(0, -1).concat([a.slice(-1)[0].concat(x)]),
[]
)
但是对我来说,它的可读性要差很多。
答案 1 :(得分:0)
您并不是真正意义上的分组,我会做这样的事情:
const originalData = [
{
"name": "locMLR",
"value": "6485183463415278686"
},
{
"name": "dayDate",
"value": "04/29/2019"
}, {
"name": "intStps",
"value": "1"
},
{
"name": "intPkgs",
"value": "2"
},
{
"name": "locMLR",
"value": "6485183463415278686"
},
{
"name": "dayDate",
"value": "04/30/2019"
}, {
"name": "intStps",
"value": "1"
},
{
"name": "intPkgs",
"value": "2"
},
{
"name": "locMLR",
"value": "6485183463415278686"
},
{
"name": "dayDate",
"value": "05/01/2019"
}, {
"name": "intStps",
"value": "1"
},
{
"name": "intPkgs",
"value": "2"
},
{
"name": "locMLR",
"value": "6485183463415278686"
},
{
"name": "dayDate",
"value": "05/02/2019"
}, {
"name": "intStps",
"value": "1"
},
{
"name": "intPkgs",
"value": "2"
},
{
"name": "locMLR",
"value": "6485183463415278686"
},
{
"name": "dayDate",
"value": "05/03/2019"
}, {
"name": "intStps",
"value": "1"
},
{
"name": "intPkgs",
"value": "2"
}
];
const groupedData = [];
let groupedItem = [];
originalData.forEach((data, index) => {
groupedItem.push(data);
if (index % 4 === 3) {
groupedData.push(groupedItem);
groupedItem = [];
}
});
console.log(groupedData);