gpt4 book ai didi

javascript - 使用 ramdajs 将对象转换为对象数组

转载 作者:行者123 更新时间:2023-11-29 10:37:48 25 4
gpt4 key购买 nike

我正在编写一个程序,计算文件中的字数。

假设对象是这样的:

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

我想做到:

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

我可以通过命令式编程实现目标:循环对象...

而且我查看了文档和谷歌,但找不到任何适合的方法:(

谢谢

最佳答案

这可以通过使用 R.toPairs 来实现和 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"}]

关于javascript - 使用 ramdajs 将对象转换为对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34025673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com