I have a transformer helper function. It reduces over the array and transform key/value pairs. At the end of the loop there is the key 'EXAMPLE1' exists and I should insert two objects after the first and the second array(result) items. Right now, it works as expected, but I have concerns - is it a good approach for using splice inside reduce in my case?
const commonData = Object.entries(settings).reduce((result, [currentKey, currentValue]) => {
if (currentKey === SETTINGS_FIELDS.EXAMPLE1) {
const type = {
key: myKey,
value: myValue,
}
const code = {
key: myKey,
value: myValue,
}
const insertedData = result.splice(2, 0, type, code)
return [...result, ...insertedData]
}
return result.concat({ key: transformToUpperCase(currentKey), value: currentValue })
}, [])
return [...commonData]
}
```
splice()
on the array directly. Do you want to do it only if thesettings
object contained the keyEXAMPLE1
? Then do it. What do you mean by "at the end of the loop"? \$\endgroup\$