msdark msdark - 10 months ago 113
Javascript Question

Convert array of objects to plan object using Ramda

How can I convert an array of objects to a plain object?
where each item of the array is an object with only one key:value pair and the key have an unknown name.

I have this

const arrayOfObject = [
{KEY_A: 'asfas'}
{KEY_B: 'asas }
let result = {}
const each = R.forEach((item) => {
const key = R.keys(item)[0]
result[key] = item[key]
return result

But I dislike that solution because the
is using a global variable
and i'm not sure how to avoid side effects here.


Ramda has a function built-in for this, mergeAll.

const arrayOfObject = [
     {KEY_A: 'asfas'}
    ,{KEY_B: 'asas' }

//=> {"KEY_A": "asfas", "KEY_B": "asas"}