ES6 Destructuring with Objects and Arrays

Complex Destructuring examples with both objects and arrays together in one complex object (or array!).

by Paweł Niemczyk

JavaScript

const meth = {
  address: 'a',
  addressLine2: 'b',
  isAddressExtended: true,
  postalCode: 'c',
  city: 'e',
  country: 'f',
  state: 'z',
  isBusinessAddress: true
}


const getPayoutMethodAddres = (method, businessAddress) => {
  if (method.isBusinessAddress && businessAddress) {
    return businessAddress
  } else {
    return {
      address: method.address,
      addressLine2: method.addressLine2,
      isAddressExtended: method.isAddressExtended,
      postalCode: method.postalCode,
      city: method.city,
      country: method.country,
      state: method.state
    }
}
}

console.log(getPayoutMethodAddres(meth, { a: 1 }));