5

I want to stop using lodash.js and switch to ramda.js but I don't see any function like _.toArray() for objects, is there something like this in ramda that I should compose or should I continue using lodash for these functions (and possibly more cases that I have not run into yet.)

For example In lodash if you have an Object like :

{"key1": {"inner": "val"}, "key2" : {"inner": "val"}}

you can convert it to an array like this:

[{"inner": "val"}, {"inner": "val"}]

using the function _.toArray()

simplesthing
  • 3,034
  • 3
  • 15
  • 16
  • 1
    `_.toArray() for objects` - Hmmm, can you give a sample? – thefourtheye May 08 '15 at 18:40
  • IIRC, `_.toArray` doesn't work on plain objects, only array-like objects (NodeList, ArgumentList, etc) – Evan Davis May 08 '15 at 18:46
  • 1
    @simplesthing Nope. It gives only the values, like this `[ { inner: 'val' }, { inner: 'val' } ]` and you can do that with [`values`](http://ramdajs.com/docs/#values) – thefourtheye May 08 '15 at 18:58
  • @thefourtheye you're correct I can edit the example, but the question is the same, is there a compose or function using ramda that I can use? – simplesthing May 08 '15 at 18:59
  • Sorry I did not see the values response, thank you @thefourtheye if you move your comment into an answer I can mark it as correct. – simplesthing May 08 '15 at 19:02
  • IIRC, `_.toArray` is mostly designed for taking the arguments object and _really_ making it an array so you can pass those as arguments to apply. I don't think it was designed for the purpose above, its probably just coincidental that it works. – Jed Schneider May 29 '15 at 13:48

2 Answers2

10

Well, Ramda has values, which seems to be what you're looking for:

var obj = {"key1": {"inner": "val"}, "key2" : {"inner": "val"}};
R.values(obj); //=> [{"inner": "val"}, {"inner": "val"}]

But it's pretty unclear from the lodash documentation, what kinds of values _.toArray function accepts, so this might not be a complete replacement.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • 1
    I imagine either [`R.values`](http://ramdajs.com/docs/#values) or [`R.toPairs`](http://ramdajs.com/docs/#toPairs) is what the OP is after. – davidchambers May 08 '15 at 21:08
2

Mb vanilla.js will help you? :) ( browser support IE9+ and all other browsers )

var obj = {"key1": {"inner": "val"}, "key2" : {"inner": "val"}};
    
    
var array = Object.keys(obj || {}).map(function(key){
   return obj[key];
});
    
document.write(JSON.stringify(array, null, 4));
obenjiro
  • 3,665
  • 7
  • 44
  • 82