klewis
klewis

Reputation: 8360

object destructing with paramaters on function

If I have the following ES6 method:

function myClothes([first, second, third]) {
  return {
    first: first,
    second: second,
    third: third
  }
} 

How do I print out "sneakers pants shirt" in my console window? I have tried the following but I am still short of a solution:

console.log(myClothes({
  ['first']:'sneakers',
  ['second']:'pants',
  ['third']:'shirt'
}));

What am I doing wrong?

Many thanks!

Upvotes: 0

Views: 28

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44135

Firstly, change your function to use object destructuring. Then use a simple console.log statement inside the function:

function myClothes({first, second, third}) {
  console.log([first, second, third].join(" "));
  return {
    first: first,
    second: second,
    third: third
  }
}

console.log(myClothes({
  ['first']:'sneakers',
  ['second']:'pants',
  ['third']:'shirt'
}));
.as-console-wrapper { max-height: 100% !important; top: auto; }

Upvotes: 1

Related Questions