object.assign javascript code example
Example 1: javascript assign
const obj = { a: 1 };
const copy = Object.assign({}, obj);
console.log(copy);
Example 2: javascript clone object
var x = {myProp: "value"};
var xClone = Object.assign({}, x);
Example 3: javascript object.assign
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
console.log(returnedTarget);
Example 4: object assign
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);
console.log(returnedTarget);
Example 5: concat object
const obj1 = { food: 'pizza', car: 'ford' };
const obj2 = { animal: 'dog' };
const obj3 = { ...obj1, ...obj2 };
console.log(obj3);
Example 6: copy an object with object.assign
const data = {
user: 'CamperBot',
status: 'offline',
friends: '732,982',
};
const newObject = Object.assign({}, data, {status: 'online'})
console.log(newObject)