Hitesh Balar Hitesh Balar - 5 months ago 41
Javascript Question

Find by key and replace by value in nested json object

I have an json object It can be nested and I have an second object containing key/value pair. I want to replace the second object's value by first one by matching key both object's key.

let firstObj = {
"enquiry": {
"Lead": {
"SubLead": {
"DealerRef": "test",
"DealerFloor": "test",
"Region": "test",
"Source": {
"Special": "test",
"TestDrive": "test",
"TradeIn": "test",
"Finance": "test"
}
},
"Contact": {
"Info": {
"FirstName": "test",
"Surname": "test",
"Email": "test",
"OfficePhone": "test",
"CellPhone": "test"
}
},
"Seeks": {
"Stock": {
"Used": "test",
"Brand": "test",
"Model": "test",
"StockNr": "test"
}
}
}
}
}


Its my array

let secondObj = {
DealerRef: '18M',
DealerFloor: 'UCP',
Region: 'Western Cape',
FirstName: 'abc',
Surname: 'xyz',
Email: 'test@ctm.co.za',
OfficePhone: '2343243',
CellPhone: '2343243',
Used: '1',
Brand: 'QAE',
Model: 'test',
StockNr: 'SEDONA',
Special: '2013 Kia Sedona',
TestDrive: '0',
TradeIn: '0',
Finance: '0'
};


I have tried many ways http://jsfiddle.net/FM3qu/7/ this way i'm able to get solution in Jsfiddle, In my express application when I try to process it gives me an empty object.

I want something like this

"enquiry": {
"Lead": {
"SubLead": {
"DealerRef": "18M",
"DealerFloor": "UCP",
"Region": "Western Cape"....


Thank you

Actually I have an Node API
/api/saveLead
when request comes along with that the second Object i'm getting as body + from mongodb there data called second object. I just have to replace value with right key.

this is my real object

export function saveLead(req, res, next) {
let firstObj = {
"enquiry": {
"Lead": {
"@attribute": {
"DealerRef": "18M",
"DealerFloor": "UCP",
"Region": "Western Cape",
"Source": "Imperial Select"
},
"Contact": {
"@attribute": {
"FirstName": "test",
"Surname": "test",
"Email": "test@ctm.co.za",
"OfficePhone": "2343243",
"CellPhone": "2343243"
}
},
"Seeks": {
"@attribute": {
"Used": "1",
"Brand": "KIA",
"Model": "SEDONA",
"StockNr": "Used Car",
"Special": "2013 Kia Sedona",
"TestDrive": "0",
"TradeIn": "0",
"Finance": "0"
}
}
}
}
};

let secondObj = { DealerRef: 'test',
DealerFloor: 'test',
Region: 'test',
Source: 'test',
FirstName: 'test',
Surname: 'test',
Email: 'test',
OfficePhone: 'test',
CellPhone: 'test',
Used: 'test',
Brand: 'test',
Model: 'test',
StockNr: 'test',
Special: 'test',
TestDrive: 'test',
TradeIn: 'test',
Finance: 'test' };

function update(object, data) {
function getAllKeys(o) {
Object.keys(o).forEach(function (k) {
if (typeof o[k] === 'object') {
return getAllKeys(o[k]);
}
keys[k] = o;
});
}
var keys = Object.create(null);
console.log(keys)

getAllKeys(object);

Object.keys(data).forEach(function (k) {
keys[k][k] = data[k];
});
}

update(firstObj, secondObj);
console.log(firstObj);
})

Answer

You could first save all references and then assign the data, you have.

function update(object, data) {
    function getAllKeys(o) {
        Object.keys(o).forEach(function (k) {
            if (typeof o[k] === 'object') {
                return getAllKeys(o[k]);
            }
            keys[k] = o;
        });
    }

    var keys = Object.create(null);

    getAllKeys(object);
    Object.keys(data).forEach(function (k) {
        if (keys[k] && k in keys[k]) { // check if key for update exist
            keys[k][k] = data[k];
        }
    });
}

var object = { "enquiry": { "Lead": { "SubLead": { "DealerRef": "test", "DealerFloor": "test", "Region": "test", "Source": { "Special": "test", "TestDrive": "test", "TradeIn": "test", "Finance": "test" } }, "Contact": { "Info": { "FirstName": "test", "Surname": "test", "Email": "test", "OfficePhone": "test", "CellPhone": "test" } }, "Seeks": { "Stock": { "Used": "test", "Brand": "test", "Model": "test", "StockNr": "test" } } } } },
    data = { DrNo: 666, DealerRef: '18M', DealerFloor: 'UCP', Region: 'Western Cape', FirstName: 'abc', Surname: 'xyz', Email: 'test@ctm.co.za', OfficePhone: '2343243', CellPhone: '2343243', Used: '1', Brand: 'QAE', Model: 'test', StockNr: 'SEDONA', Special: '2013 Kia Sedona', TestDrive: '0', TradeIn: '0', Finance: '0' };

update(object, data);

console.log(object);