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

时间:2018-11-29 22:09:42

标签: javascript lodash

flatMapping是一个由元素组成的数组,我从中得到:

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",
  }
},
]

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

2 个答案:

答案 0 :(得分:5)

Destructure the object(使用rest parameters)并创建一个新对象(使用spread syntax),一路丢弃所有不需要的属性(例如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);

答案 1 :(得分:1)

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链接在代码可读性等方面的优势之一。