是否有类似于lodash _.toArray的ramda.js?

时间:2022-07-07 21:30:36

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.)

我想停止使用lodash.js并切换到ramda.js,但我没有看到像对象的_.toArray()这样的函数,ramda中是否存在这样的东西,我应该编写或者我应该继续使用lodash这些函数(可能还有更多我尚未遇到的情况。)

For example In lodash if you have an Object like :

例如,在lodash中,如果你有一个像这样的对象:

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

{“key1”:{“inner”:“val”},“key2”:{“inner”:“val”}}

you can convert it to an array like this:

你可以将它转换为这样的数组:

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

[{“inner”:“val”},{“inner”:“val”}]

using the function _.toArray()

使用函数_.toArray()

2 个解决方案

#1


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

好吧,Ramda有价值,这似乎是你正在寻找的:

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.

但是从lodash文档中可以很清楚地知道_.toArray函数接受什么类型的值,所以这可能不是完全替代。

#2


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

Mb vanilla.js会帮到你吗? :)(浏览器支持IE9 +和所有其他浏览器)

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));

#1


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

好吧,Ramda有价值,这似乎是你正在寻找的:

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.

但是从lodash文档中可以很清楚地知道_.toArray函数接受什么类型的值,所以这可能不是完全替代。

#2


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

Mb vanilla.js会帮到你吗? :)(浏览器支持IE9 +和所有其他浏览器)

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));