Answer the question
In order to leave comments, you need to log in
How to update an object based on field mapping?
I have been writing in js for a long time and always considered such a task to be trivial, but in typescript I have not been able to solve it for several days.
There is an object whose properties need to be updated
const dest = {
a2: 'a-empty',
b2: 'b-empty',
c2: 'c-empty'
}
const source = {
a1: 'a-new',
c1: 'c-new'
}
const defs = {
a2: 'a-def',
b2: 'b-def',
c2: 'c-def'
}
const map = {
a2: 'a1',
b2: 'b1',
c2: 'c1'
}
for (const toKey in dest) {
const fromKey = map[toKey];
dest[toKey] = typeof source[fromKey] === 'undefined' ? defs[toKey] : source[fromKey];
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question