Elliot Chance
Elliot Chance

Reputation: 5736

ES6 deconstructing nested optional arguments?

Consider the following ES6 code:

function foo({name, address: {street, postcode}}) {
  console.log(name, street, postcode);
}

foo({name: 'John', address: {street: 'Foo', postcode: 1234}});
foo({name: 'Bob'});

The first call works as expected. However, I would like to make address optional (street and postcode would be undefined) instead of throwing an error. Is this possible?

Upvotes: 5

Views: 568

Answers (1)

Elliot Chance
Elliot Chance

Reputation: 5736

I found the solution:

function foo({name, address: {street, postcode} = {}}) {
  console.log(name, street, postcode);
}

Upvotes: 6

Related Questions