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

Viewed 64

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:

[
  {
    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?

4 Answers

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

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

You are trying to read locality property of undefined. You could use optional chaining operator to prevent the exception throwing. So, you need to use somthing like element?.location?.address?.locality instead of element.location.address.locality.

That would require writing a function that recurcively goes throught the contents of an object an returns a non-nested object. The function below is such a function.

const flattenObject = (obj) => {
    let result = {};
    for (const i in obj) {
        if ((typeof obj[i]) === 'object') {
            const temp = flattenObject(obj[i]);
            for (const j in temp) {
                result[j] = temp[j];
            }
        }
        else {
            result[i] = obj[i];
        }
    }
    return result;
};

The function can then be called on each nested object in the array. The map method of arrays would be do nicely for that step.

const result = nested.map(n => flattenObject(n))

console.table(result[0]) would produce the output below enter image description here

Related