Ramda- how to convert 2 children arrays in object to an array

Viewed 32

I have an object

const data = {
    t1: [ {"a": 1, "a1": 2}, {"b": 3, "b1": 4}, {"c": 5, "c1": 6} ], 
    t2: [ {"d": 2}, {"e": 2}, {"f": 2} ]
}

I want to convert object above into an array

[ {"a": 1, "a1": 2}, {"b": 3, "b1": 4}, {"c": 5, "c1": 6} , {"d": 2}, {"e": 2}, {"f": 2}]

I can do it with this code

const join = data.t1.concat(data.t2)

Is there any function in Ramda can do the similar task ?

1 Answers

you can simply use Array.values and Array.reduce

or if your browser/node support Array.flat you can use that

like this

const join = data => Object.values(data).reduce((res, v) => [...res, ...v])
const join2 = data => Object.values(data).flat()

const data = {
    t1: [ {"a": 1, "a1": 2}, {"b": 3, "b1": 4}, {"c": 5, "c1": 6} ], 
    t2: [ {"d": 2}, {"e": 2}, {"f": 2} ]
}

console.log(join(data))
console.log(join2(data))

Related