I have an object as follows:
{
id: "72232915",
entryCode: "999",
conjunction1: "1234567",
conjunction2: "8910111",
conjunction3: "1314151",
date: "08/02/2017"
}
I want to tranform the object into the following format:
{
id: "72232915",
entryCode: "999",
conjunctions: {
1: "1234567"
2: "8910111"
3: "1314151"
},
date: "08/02/2017"
}
Any ideas of how I can achieve the desired output?
Here is Typescript code that does it (very specific to what you are asking):
let o = {
id: "72232915",
entryCode: "999",
conjunction1: "1234567",
conjunction2: "8910111",
conjunction3: "1314151",
date: "08/02/2017"
};
const conjunctions = {};
for (const prop in o) {
const regex = /^conjunction(.+)$/;
const matches = prop.match(regex);
if (matches?.length === 2) {
const conjunction = matches[1];
conjunctions[conjunction] = o[prop];
delete o[prop];
}
}
o = Object.assign({}, o, {conjunctions});
console.log(o);