使用Ramda增加映射数组中的数字

问题描述 投票:2回答:4

我有一个看起来像这样的数组:

[{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中实现这一点的任何想法?

javascript ramda.js
4个回答
4
投票

就像我喜欢拉姆达一样,你在这里不需要它:

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;
}, []);  

2
投票

请尝试以下方法

const reducedData = data.reduce( (prev, val) => {
  prev.sum += val.distance
  prev.datas.push({
    ...val,
    distance: prev.sum
  })
  return prev;
}, {sum: 0, datas: []})

.reduce循环数据并将每一行合并到prev中。这个脚本的最终输出是一个看起来像{ sum: number , datas: [...]}的对象,这里的sum是总距离,而datas将包含你想要的数组。

见快照:enter image description here


2
投票

如果您正在寻找利用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>

1
投票

或者你可以使用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。但如果这不是一个问题,这个代码更清晰。

© www.soinside.com 2019 - 2024. All rights reserved.