2

I have this code in my application

let {firstName, lastName, email, password} = req.body;

var data = {firstName, lastName, email, password};

What I want to do it to make it shorter if possible like this

var data = {firstName, lastName, email, password} = req.body;

is this possible?

Muhammad
  • 861
  • 1
  • 10
  • 25
  • You can Make a Function – IbraHim M. Nada Oct 22 '17 at 07:34
  • 1
    This has been discussed on the ES6 mailing list, and the seeming consensus of the luminaries on that list is that, in spite of the fact that this request has shown up at least a dozen times on Stack Overflow, it is not a common enough requirement to introduce new syntax to handle. So your two-line solution is, at the moment, the preferred way to do things here. –  Oct 22 '17 at 16:06

3 Answers3

1

You can manually set the assignment using the approach at Question or assigning to object at target

const body = {firstName: 1, lastName: 2,email: 3, password: 4};

let data = {};

({
  firstName: data.firstName,
  lasName: data.lastName,
  email: data.email,
  password: data.password
} = body);

console.log(data);
guest271314
  • 1
  • 10
  • 82
  • 156
  • 3
    Why do you use `new Object` instead of just `{}`? And what does this actually accomplish? The only way OPs question makes any sense is if he wants to filter what`s in `req.body` -- because that already is an object and he does not need any destructuring only to then make it (the same) object again. Typo in `lasName`, 2nd piece of code. Is there any less code now than compared to using destructuring and then making an object as usual? (Answer: No there isn't.) OP already does it in two simple lines of code. _All_ "solutions" posted here are more complicated than what OP already got. – Mörre Oct 22 '17 at 07:43
  • @Mörre What is issue with using `new Object`? – guest271314 Oct 22 '17 at 07:51
  • @Mörre _"All "solutions" posted here are more complicated than what OP already got."_ Have you considered posting that sentence as an Answer? – guest271314 Oct 22 '17 at 07:53
  • 1
    https://stackoverflow.com/q/4597926/1048572 – Bergi Oct 22 '17 at 15:57
  • @Bergi And https://stackoverflow.com/questions/4597926/what-is-the-difference-between-new-object-and-object-literal-notation#comment33772150_5513376 ? – guest271314 Oct 22 '17 at 16:02
  • @guest271314 No. – Bergi Oct 22 '17 at 16:09
  • How is `Object.create()` different from `new Object` and object literal? – guest271314 Oct 22 '17 at 16:11
1

If you're using ES6+ you could use the object spread syntax like this:

const { ...data } = req.body;

If you ain't, you could create the object manually:

const { firstName, lastName, email, password } = req.body;
const data = {
   firstName: firstName,
   lastName: lastName,
   email: email,
   password: 
};
codejockie
  • 5,692
  • 3
  • 29
  • 36
  • 1
    Actually, both of these are ES6 solutions – nadavvadan Oct 22 '17 at 07:46
  • both work with ES6, but the formal won't work in a pre ES6 environment. – codejockie Oct 22 '17 at 07:53
  • Neither would work on ES5. – nadavvadan Oct 22 '17 at 07:54
  • There is no spread operator, it's spread syntax - but you're using *rest* syntax anyway. And rest syntax for objects is *not* part of ES6. – Bergi Oct 22 '17 at 10:25
  • I don't understand how `const { ...data } = req.body; ` is different, or better, than `const data = { ...req.body };`, and in any case this does not solve the OP's issue of wanting a subset of properties. –  Oct 22 '17 at 16:03
1

If you have access to lodash you can use the pick method provided by lodash.

var data = _.pick(req.body, ['firstName', 'lastName', 'email', 'password']);
shawon191
  • 1,645
  • 11
  • 25