js object.assign code example
Example 1: javascript assign
const obj = { a: 1 };
const copy = Object.assign({}, obj);
console.log(copy);
Example 2: 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 3: 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 4: object.assign react js
'use strict';
let first = {name: 'Tony'};
let last = {lastName: 'Stark'};
let person = Object.assign(first, last);
ChromeSamples.log(person);
ChromeSamples.log(first);
let a = Object.assign({foo: 0}, {bar: 1}, {baz: 2});
ChromeSamples.log(a);
let b = Object.assign({foo: 0}, {foo: 1}, {foo: 2});
ChromeSamples.log(b);
let obj = {person: 'Thor Odinson'};
let samp = {person: 'ashok'};
let clone = Object.assign({}, obj, samp);
ChromeSamples.log(clone);
Example 5: 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)
Example 6: Object.assign()
const obj1 = {
a: 5,
b: 2
}
const obj2 = Object.assign({a:6 d:7}, obj1);
console.log(obj2);