The below code achieves desired output. Is there a more elegant way to do this?
For example, is there some native javascript function like flatMap
etc that would help?
(I know I could get rid of the intermediate variable pieces
).
const config = {
fieldName1: {
validation: "schema1",
value: "abcvalue here"
},
fieldName2: {
validation: "schema2",
value: "abcvalue here"
},
}
// Desired output:
// {
// fieldName1: "schema1",
// fieldName2: "schema2",
// ...
// }
const extractValidation = (config) => {
const pieces = Object.entries(config).map(
([key, val]) => ({
[key]: val.validation
})
)
return Object.assign({}, ...pieces)
}
extractValidation(config)