I have an array of objects to be transformed and merged into a single string with array.reduce
.
const arr = [{id: 7, task: "foo"}, {id: 22, task: "bar"}]
The result should be 7. foo, 22. bar
If I write this code, it will work but produce , 7. foo, 22.bar
:
arr.reduce((pre,cur)=> pre + `, ${cur.id}. ${cur.task}`, '')
How can I properly do this without the extra comma, preferably only in FP?
Is reduce a requirement? Map is easier to understand and read.
arr.map(o => `${o.id}. ${o.task}`).join(',')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With