How to map values of one object to other if keys matched, trying to assign values here additionalData.value =userParams[prop] ? userParams[prop] : ''; however it pushing empty string any idea what has implemented wrong here ? or any better approach to achieve this task
const getParams = (userParams) => {
const interactionData = [];
const interactionDataDict = {
firstName: [{key: "FIRST_NAME", defaultValue: null}],
lastName: [{key: "LAST_NAME", defaultValue: null}],
SourceSystem: [{key: "SOURCE_SYSTEM", defaultValue: null}]
};
for (const prop in interactionDataDict) {
if (prop) {
for (const val of interactionDataDict[prop]) {
const additionalData = {};
additionalData.key = val.key;
if (val.defaultValue) {
additionalData.value = val.defaultValue;
}
else {
additionalData.value =
userParams[prop] ? userParams[prop] : '';
}
interactionData.push(additionalData);
}
}
}
return interactionData;
}
const args = {
firstName: "John",
lastName: "Hayne",
sourceSystem: "HTS"
}
console.log(getParams(args))
expected output
[{"key":"FIRST_NAME","value":"John"},{"key":"LAST_NAME","value":"Hayne"},{"key":"SOURCE_SYSTEM","value":"HTS"}]
interactionDataDictarrays?interationDataDict. Why is the keySourceSystemcapitalized and the others are not. If you change, this, I think your problem goes away.interactionDataDict.firstNamethe single-element array[{key: "FIRST_NAME", defaultValue: null}]instead of the plain object{key: "FIRST_NAME", defaultValue: null}?