Search code examples
javascriptlodash

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


I am flatMapping an array of elements where I am getting this:

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

It returns this:

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

So at the end I need something like this:

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

But I need to make billing item part of that same object, how else can I flatten it more?


Solution

  • Destructure the object (using rest parameters) and create a new object (using the spread syntax) discarding any unwanted properties (like id) along the way.

    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);

    If you've got an array of these objects just use map to return a new array of new objects.

    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);