RuNpiXelruN RuNpiXelruN - 21 days ago 5
Javascript Question

Javascript/Ramda: How to make the following code functional

Hi I have the following object structure,

const usersList = {
NFr9F4WbBxR4H5ajolbS6q0skPF2: {
name: "justin davidson",
uid: "NFr9F4WbBxR4H5ajolbS6q0skPF2"
},
asas9F4WbBxR4H5ajolbS6q0sasF2: {
name: "sawyer davidson",
uid: "asas9F4WbBxR4H5ajolbS6q0sasF2"
}
}


It has a user ID as key, and it's user object nested within. I want to store the inner user data. I've been using Ramda JS and have done so by doing the following,

let x = []
const y = R.keys(usersList).forEach((uid) => {
x.push(usersList[uid])
return x
})


which returns

[{"name":"justin davidson","uid":"NFr9F4WbBxR4H5ajolbS6q0skPF2"},
{"name":"sawyer davidson","uid":"asas9F4WbBxR4H5ajolbS6q0sasF2"}]


..however I'd like achieve the same in a purely functional way. What would be the best approach here? I'm guessing
compose and map
but I can't seem to work it out. Looking for a little direction.

Thanks

Answer

Just use map instead of forEach:

const x = R.keys(usersList).map((uid) => usersList[uid])

It looks like there's also a values method that does what you want:

const x = R.values(usersList)