从另一个数组更新一个数组中的 属性 的功能方法?
Functional way to update a property in one array from another array?
我正在尝试进行函数式编程,在创建了一些纯函数之后,我有两个数组,我需要在其中创建一个新数组,这是第一个数组,其中一个属性是从第二个数组更新而来的。
我有一个命令数组和一个秒整数数组。
命令数组有这样的对象:
var SerialCommand = function(serialString, waitTime) {
this.serialString = serialString;
this.waitTime = waitTime;
}
Seconds 只是一个整数数组,其元素数量与 commands 数组相同。
我最终想要的是一个数组,其中 commands 数组的每个元素都有以下更新:
command.waitTime = numSeconds;
我创建了以下函数:
var updateSeconds = function(command, numSeconds) {
command.waitTime = numSeconds;
return command;
}
但不确定如何将其与数组组合。
我正在为我的库使用 Ramda,但任何其他库的帮助都可以。
R.zipWith() 似乎适合您。
http://ramdajs.com/0.18.0/docs/#zipWith
我正在尝试进行函数式编程,在创建了一些纯函数之后,我有两个数组,我需要在其中创建一个新数组,这是第一个数组,其中一个属性是从第二个数组更新而来的。
我有一个命令数组和一个秒整数数组。
命令数组有这样的对象:
var SerialCommand = function(serialString, waitTime) {
this.serialString = serialString;
this.waitTime = waitTime;
}
Seconds 只是一个整数数组,其元素数量与 commands 数组相同。
我最终想要的是一个数组,其中 commands 数组的每个元素都有以下更新:
command.waitTime = numSeconds;
我创建了以下函数:
var updateSeconds = function(command, numSeconds) {
command.waitTime = numSeconds;
return command;
}
但不确定如何将其与数组组合。
我正在为我的库使用 Ramda,但任何其他库的帮助都可以。
R.zipWith() 似乎适合您。 http://ramdajs.com/0.18.0/docs/#zipWith