繁体   English   中英

如何将带有2个嵌套对象的数组弄平并使其成为一个

[英]How to flatten an array with 2 nested objects and make it to one

我在映射一个元素数组时得到了这个:

tableRowsItems = _.flatMap(data.SoftLayerCancellationRequests, 'items');

它返回以下内容:

[
  {
    "id": 11705294,
    "billingItemId": 361643044,
    "cancellationRequestId": 17289674,
    "immediateCancellationFlag": true,
    "scheduledCancellationDate": null,
    "serviceReclaimStatusCode": "COMPLETE",
    "billingItem": {
      "id": 361643044,
      "recurringFee": 0,
      "description": "Storage as a Service",
      "cancellationDate": "2018-11-27T10:20:42-06:00",
      "domainName": null,
      "hostName": null,
      "item": {
        "id": 9571,
        "description": "Storage as a Service",
        "keyName": "STORAGE_AS_A_SERVICE",
        "longDescription": null,
        "units": "N/A",
      }
    }
  },
  {
    "id": 11705292,
    "billingItemId": 361643052,
    "cancellationRequestId": 17289672,
    "immediateCancellationFlag": true,
    "scheduledCancellationDate": null,
    "serviceReclaimStatusCode": "COMPLETE",
    "billingItem": {
      "id": 361643052,
      "recurringFee": 0,
      "description": "Storage as a Service",
      "cancellationDate": "2018-11-27T10:18:18-06:00",
      "domainName": null,
      "hostName": null,
      "item": {
        "id": 9571,
        "description": "Storage as a Service",
        "keyName": "STORAGE_AS_A_SERVICE",
        "longDescription": null,
        "units": "N/A",
      }
    }
  }
]

所以最后我需要这样的东西:

[
{
  "id": 11705294,
  "billingItemId": 361643044,
  "cancellationRequestId": 17289674,
  "immediateCancellationFlag": true,
  "scheduledCancellationDate": null,
  "serviceReclaimStatusCode": "COMPLETE",
  "recurringFee": 0,
  "description": "Storage as a Service",
  "cancellationDate": "2018-11-27T10:20:42-06:00",
  "domainName": null,
  "hostName": null,
  "item": {
  "id": 9571,
    "description": "Storage as a Service",
    "keyName": "STORAGE_AS_A_SERVICE",
    "longDescription": null,
    "units": "N/A",
  }
},
]

但是我需要使计费项目成为同一对象的一部分,我还可以进一步展平它吗?

分解对象 (使用rest参数 )并创建一个新对象(使用spread语法 ),并丢弃所有不需要的属性(例如id)。

 const obj = {"id":11705294,"billingItemId":361643044,"cancellationRequestId":17289674,"immediateCancellationFlag":true,"scheduledCancellationDate":null,"serviceReclaimStatusCode":"COMPLETE","billingItem":{"id":361643044,"recurringFee":0,"description":"Storage as a Service","cancellationDate":"2018-11-27T10:20:42-06:00","domainName":null,"hostName":null,"item":{"id":9571,"description":"Storage as a Service","keyName":"STORAGE_AS_A_SERVICE","longDescription":null,"units":"N/A"}}}; // Extract `id` from billingItem, and assign all other // billingItem properties to `restItem` using rest parameters // Assign all the other (non-billingTime) properties of obj // to the variable `rest` const { billingItem: { id, ...restItem }, ...rest } = obj; // Spread `rest` and `restItem` back out to create // the new properties of the new object. Note: we haven't // added that id here - we've plucked it from the object and // discarded it const newObj = { ...rest, ...restItem }; console.log(newObj); 

如果您有这些对象的数组,只需使用map返回一个新的新对象数组。

 const arr = [{"id":11705294,"billingItemId":361643044,"cancellationRequestId":17289674,"immediateCancellationFlag":true,"scheduledCancellationDate":null,"serviceReclaimStatusCode":"COMPLETE","billingItem":{"id":361643044,"recurringFee":0,"description":"Storage as a Service","cancellationDate":"2018-11-27T10:20:42-06:00","domainName":null,"hostName":null,"item":{"id":9571,"description":"Storage as a Service","keyName":"STORAGE_AS_A_SERVICE","longDescription":null,"units":"N/A"}}}]; const newArr = arr.map(obj => { const { billingItem: { id, ...restItem }, ...rest } = obj; return { ...rest, ...restItem }; }); console.log(newArr); 

对于lodash,这是.omit .extend

 const data = [ { "id": 11705294, "billingItemId": 361643044, "cancellationRequestId": 17289674, "immediateCancellationFlag": true, "scheduledCancellationDate": null, "serviceReclaimStatusCode": "COMPLETE", "billingItem": { "id": 361643044, "recurringFee": 0, "description": "Storage as a Service", "cancellationDate": "2018-11-27T10:20:42-06:00", "domainName": null, "hostName": null, "item": { "id": 9571, "description": "Storage as a Service", "keyName": "STORAGE_AS_A_SERVICE", "longDescription": null, "units": "N/A", } } }, { "id": 11705292, "billingItemId": 361643052, "cancellationRequestId": 17289672, "immediateCancellationFlag": true, "scheduledCancellationDate": null, "serviceReclaimStatusCode": "COMPLETE", "billingItem": { "id": 361643052, "recurringFee": 0, "description": "Storage as a Service", "cancellationDate": "2018-11-27T10:18:18-06:00", "domainName": null, "hostName": null, "item": { "id": 9571, "description": "Storage as a Service", "keyName": "STORAGE_AS_A_SERVICE", "longDescription": null, "units": "N/A", } } } ] const result = _.flatMap(data, x => _.omit(_.extend(x, {item: x.billingItem.item}), ['billingItem'])) console.log(result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script> 

lodash chaining是更具可读性的示例:

 const data = [ { "id": 11705294, "billingItemId": 361643044, "cancellationRequestId": 17289674, "immediateCancellationFlag": true, "scheduledCancellationDate": null, "serviceReclaimStatusCode": "COMPLETE", "billingItem": { "id": 361643044, "recurringFee": 0, "description": "Storage as a Service", "cancellationDate": "2018-11-27T10:20:42-06:00", "domainName": null, "hostName": null, "item": { "id": 9571, "description": "Storage as a Service", "keyName": "STORAGE_AS_A_SERVICE", "longDescription": null, "units": "N/A", } } }, { "id": 11705292, "billingItemId": 361643052, "cancellationRequestId": 17289672, "immediateCancellationFlag": true, "scheduledCancellationDate": null, "serviceReclaimStatusCode": "COMPLETE", "billingItem": { "id": 361643052, "recurringFee": 0, "description": "Storage as a Service", "cancellationDate": "2018-11-27T10:18:18-06:00", "domainName": null, "hostName": null, "item": { "id": 9571, "description": "Storage as a Service", "keyName": "STORAGE_AS_A_SERVICE", "longDescription": null, "units": "N/A", } } } ] const result = _.flatMap(data, x => _(x) .extend({item: x.billingItem.item}) .omit(['billingItem']) .value() ); console.log(result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script> 

这几乎可以准确地读出正在发生的事情,这是lodash链在代码可读性等方面的优势之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM