SyntaxHighlighter

Wednesday, June 15, 2016

Redis Ordered Set WITHSCORES into JSON Object (NodeJS)

A note for myself before I stop using this ordered set.

Redis Ordered Sets can return the members of a set with their scores. Its an option on the zrange command, so to get all the values in an ordered set with their scores:
`redis.client.zrange( key, 0, -1, 'WITHSCORES')`

Of course, the returned array is pretty specific, something like:
user1 135000 user2 384000 . . .

Using the handy lodash library, I wanted to keep track of this little hack:
//this is a synchronous operation and would block the event loopvar testScores = _.fromPairs(_.chunk(testValuePairs, 2));

I've seen it done with for-loops that skip every other array item, but I liked the single line of this approach. It does go through the array twice, once to turn into paired array, and once through that array to create the object, but for my purposes the array sizes were going to be small and the succinctness was useful.

No comments:

Post a Comment