RuNpiXelruN RuNpiXelruN - 1 year ago 62
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) => {
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.


Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download