我尝试使用Ramda编写代码,仅使用原始对象的
id
和comment
键来生成新的数据结构。我对Ramda很陌生,但我有使用Python进行类似编码的经验。
给定以下初始数据结构...const commentData = {
'30': {'id': 6, 'comment': 'fubar', 'other': 7},
'34': {'id': 8, 'comment': 'snafu', 'other': 6},
'37': {'id': 9, 'comment': 'tarfu', 'other': 42}
};
我想把它转化为这样...
{
'6': 'fubar',
'8': 'snafu',
'9': 'tarfu'
}
我在Ramda的菜谱中找到了以下 例子,它与要求相似...
const objFromListWith = R.curry((fn, list) => R.chain(R.zipObj, R.map(fn))(list));
objFromListWith(R.prop('id'), R.values(commentData));
但它所返回的值包括整个原始对象作为值......
{
6: {id: 6, comment: "fubar", other: 7},
8: {id: 8, comment: "snafu", other: 6},
9: {id: 9, comment: "tarfu", other: 42}
}
如何把值仅保留为它们的comment
键对应的值?
我不需要使用参考书中提供的代码。如果有更好(更简单、更短、更高效)的代码可以得到所需结果,我会很乐意使用。