0

Let's say I have an Object

var bar = {hi: 1, there: 2};

I would like to, at the end of the function, return the same object that was passed while at the same time doing a destructuring assignment in the function param.

It might look like this:

function foo({hi, there}){
    //logic with variables "hi" and "there"
    return ...arguments;
}

and have the return value be the same as bar; for obvious reasons the spread operator doesn't work in this context, but I'm wondering if there is a simple way to do this or something similar.

anonrose
  • 1,271
  • 3
  • 12
  • 19

1 Answers1

1

Name and destructure separately:

function foo(obj) {
    let {hi, there} = obj;
    // logic with variables "hi" and "there"
    return obj;
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Thanks, I was wondering if there was a way to do this w/o having that second line. – anonrose Nov 17 '17 at 00:11
  • @anonrose: There’s nothing that avoids doing them separately. You can abuse a second parameter to get them on the same *line* with `function foo(obj, {hi, there} = obj)`, but I wouldn’t. Another fun quirk is `function foo({obj: {hi, there}, obj})` and `foo({obj: bar})`, but again… – Ry- Nov 17 '17 at 00:15