简体   繁体   English

我从 API 返回了这个示例响应。 我正在使用 Lodash 的 _.groupBy 将数据转换为分组的 output。 样本响应就像

[英]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.

相关问题 我正在从 API 请求数据。 目前,我以对象数组的形式获取数据,但无法在浏览器上呈现输出 - I am requesting data from an API. Currently, I am getting data as an array of objects but I'm not able to render output on the browser 我有一个API,我想从api响应中获取数据 - I have an API, I want to get the data from api response 我如何使用脚本获取Plaid API返回的响应 - how can I get response returned by Plaid api using script React JS Axios Response is not populating the array of objects returned by api. 响应返回了 20 个对象 - React JS Axios Response is not populating the array of objects returned by api. There are 20 objects returned by the response 我向Twitter API提出了请求。 如何从PHP脚本获得对JavaScript的JSON响应? - I made a request to the Twitter API. How can I get the JSON response from my PHP script to my JavaScript? 我有这样的JSON响应,由于某种原因我无法访问该值 - I have a JSON response like this, and I'm having trouble access the value for some reason 如何在我的 API 上返回来自 Auth0 的 API 的响应数据? - How can I return the response data from Auth0's API on my API? 我如何将 output 来自 API 调用的响应放入页面? - How can I output the response from an API call into the page? 如何将对象数组从 API 响应转换为 object - How do I convert array of Objects from an API Response into an object 如何将来自 api 的响应转换为图像 - How can I convert the response coming from the api to image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM