Update an Object - 02

Update an object:

With Object.assign():


let person = {
    name: "Mozahedul",
    title: "student",
};

let man = Object.assign({}, person, {name: "john", age: 30});
console.log(man); // Result: 54;

With spread operator : 


let person = {
    name: "Mozahedul",
    title: "student",
};

let man = {...person, name: "john", age: 30}
console.log(man); // Result: 54;


With spread operator for nested object: 


const person = {
    name: "Mozahedul",
    title: "student",
    address: {
        country: "USA",
        city: "New York",
    }
};

let updated = {
    ...person.
    address: {
        ...person.address,
        country: "BD",
        zipCode: 5440
    }
};

console.log(updated);

Post a Comment

Previous Post Next Post