gpt4 book ai didi

javascript - Ramda JS : How to perform a map where I call R. 替换每个对象的给定属性?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:42:28 27 4
gpt4 key购买 nike

给定以下数据:

const my_data = [
{
name: "John",
age: 22
},
{
name: "Johnny",
age: 15
},
{
name: "Dave",
age: 27
}
]

我想转换数据,将每个名称属性中的子字符串“John”替换为“Ben”,如下所示:

[
{
name: "Ben",
age: 22
},
{
name: "Benny",
age: 15
},
{
name: "Dave",
age: 27
}
]

我想以适当的功能方式这样做(我认为这是免费的,但我仍在学习),所以我可以在管道中重用它,比如先按年龄减少然后进行替换,或者做先替换再排序。我将如何使用 Ramda 函数执行此操作?

var fix_names = ???
var fixed_data = R.map( fix_names, my_data );

最佳答案

R.map(R.over(R.lensProp('name'), R.replace('John', 'Ben')))(my_data)

参见 R.overR.lensProp .

关于javascript - Ramda JS : How to perform a map where I call R. 替换每个对象的给定属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37378900/

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