I have a dictionary as
var dict1= { 'Pname1-type1-UOM1': 'D43',
'Pname1-type2-UOM1': 'D59',
'Pname1-type3-UOM2': 'D75',
'Pname1-type4-UOM2': 'D91'
}
and another dictionary as
var dict2= { 'F43': 'Yearly',
'F59': 'Monthly',
'F75': 'Quarterly',
'F91': 'Monthly'
}
What I want is a dict as
{ 'Pname1-type1-Yearly-UOM1': 'D43',
'Pname1-type2-Monthly-UOM1': 'D59',
'Pname1-type3-Quarterly-UOM2': 'D75',
'Pname1-type4-Monthly-UOM2': 'D91'
}
What is similar between dict1
and dict2
is the first key value pair of dict1
relates to first key value pair of dict2
and the others also respectively.
Another similarity would be the value of dict1
D43
will have key in dict2
as F43
, so the D's are F's with same number following them
Please help!
CodePudding user response:
Although I'm not sure whether I could correctly understand your question, in your situation, how about the following sample script?
Sample script:
var dict1 = {
'Pname1-type1-UOM1': 'D43',
'Pname1-type2-UOM1': 'D59',
'Pname1-type3-UOM2': 'D75',
'Pname1-type4-UOM2': 'D91'
};
var dict2 = {
'F43': 'Yearly',
'F59': 'Monthly',
'F75': 'Quarterly',
'F91': 'Monthly'
};
var obj = Object.entries(dict1).reduce((o, [k, v]) => (o[v] = k, o), {});
var res = Object.entries(dict2).reduce((o, [k, v]) => {
var key = k.replace("F", "D");
var temp = obj[key].split("-");
temp.splice(2, 0, v);
o[temp.join("-")] = key;
return o;
}, {});
console.log(res);
- When this script is run, the following object is obtained.
{
"Pname1-type1-Yearly-UOM1":"D43",
"Pname1-type2-Monthly-UOM1":"D59",
"Pname1-type3-Quarterly-UOM2":"D75",
"Pname1-type4-Monthly-UOM2":"D91"
}