我卡在一个点上了。我有一个consolidatedObj
对象
const consolidatedObj = {
"flag": "Data Concept",
"UpdateDC": {
"id": 732,
"oId": 695112,
"cType": "DCON",
"clientId": 1,
"aId": 236,
"fType": "DAT_OWN",
"details_1": {},
"details_2": {}
}
}
还有anotherPayload
对象我需要把它附加到合并对象
const anotherPayload = {
flag: 'LOB',
UpdateLOB: {
assessmentId: +this.assessmentId,
lobId: +this.lob['id'],
}
};
下面O/p
const consolidatedObj = {
"flag": "Data Concept | LOB",
"UpdateDC": {
"id": 732,
"oId": 695112,
"cType": "DCON",
"clientId": 1,
"aId": 236,
"fType": "DAT_OWN",
"details_1": {},
"details_2": {}
},
"UpdateLOB": {
"assessmentId": +this.assessmentId,
"lobId": +this.lob['id'],
}
}
如何将下面的对象添加到现有的合并对象中,并附加由pipe(|)
符号分隔的标志值
const consolidatedObj = {
"flag": "Data Concept",
"UpdateDC": {
"id": 732,
"oId": 695112,
"cType": "DCON",
"clientId": 1,
"aId": 236,
"fType": "DAT_OWN",
"details_1": {},
"details_2": {}
}
}
const anotherPayload = {
flag: 'LOB',
UpdateLOB: {
assessmentId: 123,
lobId: 321,
}
};
function mergeObjects() {
let responseObj = {};
for (let arg = 0; arg < arguments.length; arg++) {
for (let prop in arguments[arg]) {
// if property is str and already exist in responseObj = concat with existing
if (responseObj[prop] && typeof responseObj[prop] === 'string') {
responseObj[prop] += ' | ' + arguments[arg][prop];
} else {
// else just add obj prop to response
responseObj[prop] = arguments[arg][prop];
}
}
}
return responseObj;
}
console.log(
mergeObjects(consolidatedObj, anotherPayload)
);