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

Time:08-28

When accessing a public register API, I receive more information than I need. 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:

[
  {
    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,
        endDate: element.endDate,
        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?

CodePudding user response:

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

CodePudding user response:

Since it looks like you don't know what the outer key will be for the object with the address property, if the object will always have 4 properties, when destructuring, you can use rest syntax to collect the final property into a single object, and then take that object's values to get to the address.

const input=[{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"}}}];

const output = input.map(({
  startDate,
  expiryDate,
  party,
  ...rest
}) => ({
  startDate,
  expiryDate,
  partyName: party.name,
  location: Object.values(rest)[0].address.locality,
}));
console.log(output);

CodePudding user response:

You are trying to read locality property of undefined. So, you need to use somthing like element?.location?.address?.locality instead of element.location.address.locality.

  • Related