1 year ago

#383121

test-img

sravan ganji

how to handle Destructuring assignment if the property is not available?

i have a function to format the date object (especially zip code (converting from 12345-678 to 12345)), because i need to compare 2 address objects(for ex: physical address and mailing address ).

 formatAddress(address: any) {
    const { country , addressType , ...addressCopy } = address;
    const { postalCode } = addressCopy;
    const formattedPostalCode = postalCode && postalCode.split('-')[0];
    addressCopy.postalCode = formattedPostalCode;
    return addressCopy;
  }

i am destructing the address and removing the country and addressType because mailing address object has the extra country , addressTypeto compare with physical address.

but when i pass the phyical address to formatAddress function , it is failing because it doesn't have country , addressType , how to fix this and can we assign undefined to country , addressType if the address doesn't have country , addressType keys.

javascript

typescript

ecmascript-6

object-destructuring

0 Answers

Your Answer

Accepted video resources