6

This is the situation:

user.username = body.username;
user.name = body.name;
user.surname = body.surname;
user.email = body.email;
user.password = body.password;
user.privilege = body.privilege;
user.pin = body.pin;
user.rfidTag = body.rfidTag;

I modified it this way and it works as expected:

for (let propt in body) { 
    user[propt] = body[propt];
}

I am wondering if there is a more elegant way to write this, maybe something that has a property check.

[UPDATE]: There are various ways to solve this.

If you don't need to retain user properties:

user = Object.assign( {}, body );

or the proposal spread property:

user = { ...body };

Otherwise to retain properties of user:

Object.assign( user, body );
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Just one pointer, if you have nested objects, it would copy reference and not actual object. You can refer following post if you have nested structure: https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript – Rajesh Dec 27 '17 at 13:48
  • Possible duplicate of [How do I correctly clone a JavaScript object?](https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object) – laurent Dec 27 '17 at 13:59
  • This is not cloning, this is about assigning properties. – user9145246 Dec 27 '17 at 14:02

3 Answers3

5

You can use Object.assign

user = Object.assign( {}, body );

Demo

var body = { user : "abc", username : "some-value" };

var user = Object.assign( {}, body );

console.log( user );

Edit

If user already has some properties (which are not in body) which you don't want to be wiped-off then

Object.assign( user, body );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • This led me to the the answer Object.assign(user, body); – user9145246 Dec 27 '17 at 13:50
  • 1
    1 caveat. This approach will copy any extra properties as well that are there in body but not in user. – Rajesh Dec 27 '17 at 13:52
  • 3
    @Rajesh You are right. In case OP wants to only copy some of the properties, then question may help https://stackoverflow.com/questions/47992783/filter-object-props-based-on-the-specified-array – gurvinder372 Dec 27 '17 at 13:53
1

You can use spread syntax ...

user = { ...body };

var body = { prop1 : "1", prop2 : "2", prop3 : "3" };
var user = { ...body };
console.log( user );
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 2
    [`...` is not an operator!](https://stackoverflow.com/questions/37151966/what-is-spreadelement-in-ecmascript-documentation-is-it-the-same-as-spread-oper/37152508#37152508) – Felix Kling Dec 27 '17 at 13:22
  • 1
    @gurvinder372 thanks for the catch... updated answer `:)` – Jordi Castilla Dec 27 '17 at 13:40
  • 1
    @JordiCastilla Just updated the snippet to use babel. Please revert if the update is not necessary – Rajesh Dec 27 '17 at 13:44
0

If you already have your user object initialized to it's default values you can use it to copy only the fields you need.

var defaultUser = {username: null, name: null, surname: null, privilege: 1|2|4}

function createUser (user, body) {
    var newUser = {}; //Object.create (user);
    for (let key in user) {
        newUser [key] = body [key] || user [key];
    }
    return newUser
}

newUser = createUser (defaultUser, body);

The Object spread operator (introduced in ES6) can be used to assign all properties of one object to another, effectively cloning it.

var user = {...body} 

However, you would not have any control over the fields being copied. So every field contained in the body will end up in you user.

This might or might not be what you're looking for.

Moritz Roessler
  • 8,542
  • 26
  • 51
  • On a sidenote, since you're on node.js and your user(s) probably will end up in a database, you might want to look into [http://docs.sequelizejs.com/](Sequelize), a node.js ORM. – Moritz Roessler Dec 27 '17 at 13:43