[英]I have this sample response returned from an API. I'm using Lodash's _.groupBy to convert the data to the grouped output. sample response is like
[
{
"airbill_pieces": 1,
"airbillnbr": 81061140,
"load_point": "IND",
"manifestnbr": 1907521,
"handled_pieces": 5,
"manifest_weight": 12548,
},
{
"airbill_pieces": 2,
"airbillnbr": 81061158,
"load_point": "IND",
"manifestnbr": 1907522,
"handled_pieces": 12,
"manifest_weight": 12368,
}
]
required response as所需的响应为
{
"data": {
"load_point": "IND",
"airbill_pieces": "sum of all "handled_pieces" / sum of all "airbill_pieces",
"manifest_weight": sum of "manifest_weight",
"manifestnbr": 1907521,
},
"children": [
{
"data": {
"airbillnbr": 81061140,
"manifest_weight": 12548,
"manifestnbr": 1907521,
"airbill_pieces": 1,
}
},
{
"data": {
"airbillnbr": 81061158,
"manifest_weight": 12368,
"manifestnbr": 1907522,
"airbill_pieces": 2
}
}
]
}
I am using the below code to get the desire output but fail to get exact format.我正在使用下面的代码来获得所需的 output 但无法获得确切的格式。 it groups the load_point as a keypair values but i need a object followed by "data" key.
它将 load_point 分组为密钥对值,但我需要 object 后跟“数据”密钥。
let result = _.chain(outboundAirbills)
.groupBy("load_point")
.pairs()
.map( (currentItem) => {
return _.object(_.zip(["data", "children"], currentItem));
})
.value();
console.log(result);
Any help will be appreciated.任何帮助将不胜感激。
Here is a version in vanilla JS, using two simple helpers, sum
which calculates the sum of an array of numbers, and group
which groups items of an array into subarrays for which the supplied function returns the same value.这是 vanilla JS 中的一个版本,使用两个简单的助手,
sum
计算数字数组的总和, group
将数组的项目分组为子数组,提供的 function 返回相同的值。 (It's like lodash's or groupBy
followed by values
, but curried, more like Ramda): (就像 lodash's 或
groupBy
后跟values
,但 curried,更像 Ramda):
const sum = (ns) => ns.reduce ((a, b) => a + b, 0) const group = (fn) => (xs) => Object.values ( xs.reduce ((a, x) => ((a [fn (x)] = (a [fn (x)] || []).concat (x)), a), {}) ) const restructure = (xs) => group (x => x.load_point) (xs).map (g => ({ data: { load_point: g [0].load_point, airbill_pieces: sum (g.map (x => x.handled_pieces)) / sum (g.map (x => x.airbill_pieces)), manifest_weight:sum (g.map (x => x.manifest_weight)), // manifestnbr: g [0].manifestnbr, }, children: g.map (({load_point, manifest_weight, manifestnbr, airbill_pieces}) => ({data: {load_point, manifest_weight, manifestnbr, airbill_pieces}}) ) })) const data = [{airbill_pieces: 1, airbillnbr: 81061140, load_point: "IND", manifestnbr: 1907521, handled_pieces: 5, manifest_weight: 12548, }, {airbill_pieces: 2, airbillnbr: 81061158, load_point: "IND", manifestnbr: 1907522, handled_pieces: 12, manifest_weight: 12368}] console.log (restructure (data))
.as-console-wrapper {max-height: 100%;important: top: 0}
We first group the data based on load_point
and then, for each group, summarize the result in data
and collect a subset of the fields inside elements of a children
array.我们首先根据
load_point
对数据进行分组,然后对于每个组,将结果汇总到data
中并收集children
数组元素内的字段子集。
If you do want the first manifestnbr
, just uncomment the relevant line.如果您确实想要第一个
manifestnbr
,只需取消注释相关行。 But it's redundant data since you have it in the first child, and it's incomplete data, since it ignores all the values after the first one;但它是冗余数据,因为您在第一个孩子中有它,而且它是不完整的数据,因为它忽略了第一个孩子之后的所有值; still, it might be necessary for your needs.
不过,它可能对您的需求是必要的。
You could definitely write this with lodash's groupBy
, but this shows how easy it is to maintain your own collection of tools.您绝对可以使用 lodash 的
groupBy
编写此代码,但这表明维护自己的工具集是多么容易。 (I'm a primary author of another collection of tools, Ramda .) Note that if you wanted to have both groupBy
(which returns an object keyed off the results of the function) and group
, which just groups the array into subarrays), you could build group
atop groupBy
trivially like this: (我是另一个工具集Ramda的主要作者。)请注意,如果您想要同时拥有
groupBy
(它返回一个 object 键控函数的结果)和group
,它只是将数组分组为子数组),你可以像这样简单地在groupBy
上建立group
:
const groupBy = (fn) => (xs) =>
xs .reduce ((a, x) => ((a [fn (x)] = (a [fn (x)] || []) .concat (x)), a), {})
const group = (fn) => (xs) =>
Object .values (groupBy (fn) (xs))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.