2426021684 2426021684 - 5 months ago 15
Javascript Question

Concise way to immutably add object to an array that is a property of an object and return the new object

Is there a more concise way to functionally add an item to an array that is a property of an object?

Imperative:

secitems.sections.push("Test")
return secitems


Functional:

const R = require("ramada")
return Object.assign({}, secitems, {
sections: R.append(
"Test",
secitems.sections
)
})


My functional version seems too long and complicated compared to the imperative version. Is there a more concise way to write it?

DJ. DJ.
Answer
const R = require('ramda')
return R.mergeWith(R.concat, secitems, { sections: ["Test"] })