3

I am writing a program, counting words in a file.

Suppose the object likes this:

{
  I: 2,
  it: 4,
  that: 1
}

And I wanna make it:

[
  { word: 'I', count: 2 }, 
  { word: 'it', count: 4 }, 
  { word: 'that', count: 1 }
]

I can achieve the goal by using imperative programming: loop the object...

And I check out the docs and google, but can't find any method fit in :(

Thanks

1 Answer 1

3

This can be achieved using R.toPairs and R.zipObj:

//    convert :: {a} -> [{ word :: String, count :: a }]
const convert = R.compose(R.map(R.zipObj(['word', 'count'])), R.toPairs);

convert({I: 2, it: 4, that: 1});
// => [{"count": 2, "word": "I"}, {"count": 4, "word": "it"}, {"count": 1, "word": "that"}]
Sign up to request clarification or add additional context in comments.

2 Comments

cool~ BTW, how should I learn ramdajs? I can't find any resources except the official site, and I can't always ask the questions in here.
The Gitter room is quite active, and the people there are very helpful. Also, Brian Lonsdorf has created many wonderful resources for learning about functional programming in JavaScript. Have a look for his various tutorials, talks, and screencasts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.