const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }
The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the target object.
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }
const user = { name: 'user', password: 'user'}
const admin = { name 'admin', rights: 'root'}
let merge = Object.assign({}, user, admin)
console.log(merge)
// {name 'admin', password: 'user', rights: 'root'}
merge = {...user, ...admin}
console.log(merge)
// {name 'admin', password: 'user', rights: 'root'}
// Object assign in javascript
let fname = { firstName : 'Black' };
let lname = { lastName : 'Panther'}
let full_names = Object.assign(fname, lname);
console.log(full_names); //{ firstName: 'Black', lastName: 'Panther' }
const obj1 = {
a: 5,
b: 2
}
const obj2 = Object.assign({a:6 d:7}, obj1);
console.log(obj2);
// output: { a: 5 , b: 2, d:7}
const newId= tours[tours.length-1].id+1;
const newTour= Object.assign({id:newId},req.body) //object.assign() is used to merge two objects.