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?

2
  • You can Make a Function Commented Oct 22, 2017 at 7: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. Commented Oct 22, 2017 at 16:06

3 Answers 3

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']);
Sign up to request clarification or add additional context in comments.

Comments

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: 
};

5 Comments

Actually, both of these are ES6 solutions
both work with ES6, but the formal won't work in a pre ES6 environment.
Neither would work on ES5.
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.
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.
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);

7 Comments

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 whats 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 What is issue with using new Object?
@Mörre "All "solutions" posted here are more complicated than what OP already got." Have you considered posting that sentence as an Answer?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.