Best Approach for Assignment Code in Node.js (ES6 style) Is there any way to improve this code? Using ES6 style i am using latest stable version of node.js.

const UserSchema = new mongoose.Schema({
date: string;
dateObj: {
    year: number,
    month: number,
    day: number
};
project: string;
task: string;
hours: number;
});

let data = new Data();
data.date = new Date(body.date);
    data.dateObj = {
        year: body.dateObj.year,
        month: body.dateObj.month,
        day: body.dateObj.day
    }
    data.project = body.project;
    data.task = body.task;
    data.hours = body.hours;
}
For flat objects (i.e. whose values are only primitive types), there's Object.assign(), which shallow-copies all enumerable keys and values from one object to the other:

Object.assign(data, body);


However, in your case, shallow-copying means the dateObj member of the body would get copied by reference, instead of creating a new object and recursively copying its keys and values.

You can also try using the spread operator ..., which was introduced recently:

let data = new Data();

Object.assign(data, {
    // Shallow-copy all keys and values from body:
    ...body,

    // Overwrite certain keys:
    date: new Date(body.date),
    dateObj: { ...body.dateObj },
});


Whether or not this code is readable or intuitive is up to you to decide.

Also, as a minor note, Object.assign returns the first object you pass to it, so instead of this:

let data = new Data();
Object.assign(data, { /* ...properties */ });


You can write this:

let data = Object.assign(new Data(), { /* ...properties */ });