Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to transform a JavaScript object into another object with cleaner formatting?

When accessing a public register API, I receive more information than I need, and sometimes the data is returned with minor variations. I would like to delete some unnecessary fields, move nested fields to the top level, and rename them. The goal is to standardise format across several different APIs, and keep the memory requirement to a minimum. Example below:

Raw object:

[
  {
    startDate: "2022/08/27",
    expiryDate: "2025/08/27",
    party: {
      type: "Business",
      name: "Irregular Expressions Inc."
    },
    location: {
      type: "Office",
      address: {
        locality: "Boston",
        postcode: "PE21 8QR"
      }
    }
  },
  {
    startDate: "2023/12/22",
    expiryDate: "2024/06/22",
    party: {
      type: "Charity",
      name: "Save the Badgers"
    },
    site: {
      type: "Office",
      address: {
        locality: "Badgerton",
        postcode: "BA6 6ER"
      }
    }
  },
]

I want to transform this into a smaller, cleaner array:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    startDate: "2022/08/27",
    expiryDate: "2025/08/27",
    partyName: "Irregular Expressions Inc.",
    location: "Boston"
  },
  {
    startDate: "2023/12/22",
    expiryDate: "2024/06/22",
    partyName: "Save the Badgers",
    location: "Badgerton"
  },
]

I have tried the below, but I’m getting an error.

module.exports = {
  testTransform: (inputArray) => {
    const outputArray = []
      
    inputArray.forEach(element => {
      outputArray.push({
        startDate: element.startDate,
        expiryDate: element.expiryDate,
        partyName: element.party.name,
        location: element.location.address.locality
      })
    })

    return JSON.stringify(outputArray, null, '  ')
  }
}
TypeError: Cannot read properties of undefined (reading 'address')

Am I going in the right direction, or is there a simpler way of doing this? I’ve searched for this type of transformation but with no luck – what am I missing?

>Solution :

You could take either location or site with logical OR || and later the proerties with optional chaining operator ?..

const
    data = [{ startDate: "2022/08/27", expiryDate: "2025/08/27", party: { type: "Business", name: "Irregular Expressions Inc." }, location: { type: "Office", address: { locality: "Boston", postcode: "PE21 8QR" } } }, { startDate: "2023/12/22", expiryDate: "2024/06/22", party: { type: "Charity", name: "Save the Badgers" }, site: { type: "Office", address: { locality: "Badgerton", postcode: "BA6 6ER" } } }],
    result = data.map(o => ({
        startDate: o.startDate,
        expiryDate: o.expiryDate,
        partyName: o.party.name,
        location: (o.location || o.site)?.address?.locality
    }));

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading