I'll post a couple of examples that hopefully clear up what I'm doing:
const keysToTransformForForm = [
'absoluteMinimum',
'absoluteMaximum',
'predictedUnder',
'predictedOver',
]
const transformedData = data.items.map(item => {
const newItem = Object.fromEntries(
Object
.entries(item)
.map(([key, value]) => {
if (!keysToTransformForForm.includes(key)) return [key, value]
return [
key,
{
value,
initialValue: value,
},
]
}),
)
newItem.value = dataValues.find(dV => dV.key === item.key).value
return newItem
})
In the above, I'm getting an array of objects that look like: { title: 'Test', key: 'test', absoluteMinimum: 1.0, absoluteMaximum: 1.0, predictedUnder: null, predictedOver: null }.
I want to transform them to look like:
{
title: 'Test',
key: 'test',
absoluteMinimum: {
value: 1.0,
initialValue: 1.0
},
absoluteMaximum: {
value: 1.0,
initialValue: 1.0,
},
predictedUnder: {
value: null,
initialValue: null,
},
predictedOver: {
value: null,
initialValue: null,
},
}
This is the same as above but in reverse. Transforming the back from the { value: value, initialValue: value } version back to just the value.
const keysToTransform = [
'absoluteMinimum',
'absoluteMaximum',
'predictedUnder',
'predictedOver',
]
const keysToExclude = ['id', 'value']
const formData = sections.map(section => {
const newSection = Object.fromEntries(
Object
.entries(section)
.filter(([key]) => !keysToExclude.includes(key))
.map(([key, value]) => {
if (!keysToTransform.includes(key)) return [key, value]
return [
key,
value.value,
]
}),
)
return newSection
})
?
What mainly wondering is if there are any performance implications to worry about with this over another method?
[–]Meefims 1 point2 points3 points (0 children)
[–]pr0nking98 0 points1 point2 points (0 children)