0

I want to copy the object of a variable to assign to another variable and only change some properties in the object of the new variable without changing the old object.

I create new object for new variable and change properties but when print out old variable is also changed. how to fix it?

let thongTinMuaXeKhachA = {
    iDluotmua:`abc129867`,
    nguoiSohuu:{
        ten: `Pham Van A`,
        tuoi: 35,
        gioTinh: `nam`,
        namSinh: 1984,
        diaChi: `ha noi`,
        cccd: 12345678,
    },
    thongTinXe: {
        tenXe: `Vinfast luxA 2.0`,
        hangXe: `Vinfast`,
        soCho: 5,
        tocDoTrungBinh: 90,
        mauSac: `Den`,
        giaBan: 1200000000,
        namSanXuat: 2019,
    },
    ngayMua: `26/01/2022`,
};
console.log(`Thong tin khach hang A:`,thongTinMuaXeKhachA);
console.log(`--------------------------`);

let thongTinMuaXeKhachB = Object.assign({},thongTinMuaXeKhachA);
thongTinMuaXeKhachB.nguoiSohuu.ten = `Pham Van B`;
console.log(thongTinMuaXeKhachA);
console.log(thongTinMuaXeKhachB);    
1
  • You will need to pick one language (Java or Javascript -- not both -- and I'm guessing you mean Javascript) and correctly format your code before we can realistically help you. Commented Dec 9, 2024 at 23:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.