我有一个看起来像这样的数组:
[{location: {…}, distance: 0}
{location: {…}, distance: 0}
{location: {…}, distance: 0.37348441209694133}
{location: {…}, distance: 0}
{location: {…}, distance: 0.4229382782128456}
{location: {…}, distance: 0}
{location: {…}, distance: 0.006098292961396555}
{location: {…}, distance: 0}
{location: {…}, distance: 0.07885846317546494}]
我想映射一个新数组,其距离值从前一个值开始递增。因此,在上面的示例中,最后一个距离值将为0.88137944644665,因为它在迭代所有对象时添加了所有值。
在Ramda.js中,我试图在映射数组时添加前一个距离值,但它不起作用,因为它未定义。我也考虑过减少没有成功。关于如何在Ramda.js中实现这一点的任何想法?
就像我喜欢拉姆达一样,你在这里不需要它:
arr.map(({ location, distance }) => ({ location, distance: distance + 1 }));
打破这种情况:
arr // your array of objects
.map( // call to Array.prototype.map
({ location, distance }, i, arr) => // function params, destructures
({ location, distance: distance + 1 })); // return new object same loc, incremented distance
由于我错过了有关聚合的部分,请使用reduce代替:
arr.reduce((aggregate, { location, distance }, i) => {
const dist = i ? aggregate[i - 1].distance : 0;
aggregate.push({ location, distance: distance + dist });
return aggregate;
}, []);
如果您正在寻找利用Ramda函数,R.scan
可以提供帮助。此函数与reduce
非常相似,但它不会返回单个汇总值,而是生成每个连续结果的列表。
const distanceL = R.lensProp('distance')
const addDistance = R.compose(R.add, R.prop('distance'))
const fn = data =>
R.scan(
(acc, next) => R.over(distanceL, addDistance(acc), next),
data[0],
R.tail(data)
)
console.log(fn([
{location: {}, distance: 0},
{location: {}, distance: 0},
{location: {}, distance: 0.37348441209694133},
{location: {}, distance: 0},
{location: {}, distance: 0.4229382782128456},
{location: {}, distance: 0},
{location: {}, distance: 0.006098292961396555},
{location: {}, distance: 0},
{location: {}, distance: 0.07885846317546494}
]))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
或者你可以使用scan
:
const {lift, scan, head, tail} = R
const xs = [
{location: {id: 1}, distance: 0},
{location: {id: 2}, distance: 0},
{location: {id: 3}, distance: 0.37348441209694133},
{location: {id: 4}, distance: 0},
{location: {id: 5}, distance: 0.4229382782128456},
{location: {id: 6}, distance: 0},
{location: {id: 7}, distance: 0.006098292961396555},
{location: {id: 8}, distance: 0},
{location: {id: 9}, distance: 0.07885846317546494}
]
const accumDist = lift(scan((agg, x) => ({...x, distance: x.distance + agg.distance})))(head, tail)
console.log(accumDist(xs))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
这不适用于空列表。如果这是一个问题,我们可以通过跳过lift(...)(head, tail)
并仅使用带有初始值的scan
来改变它,然后取tail
。但如果这不是一个问题,这个代码更清晰。