【问题标题】:return the last max value out of multiple arrays in coffeescript返回coffeescript中多个数组的最后一个最大值
【发布时间】:2014-11-27 13:22:00
【问题描述】:

我正在寻找在咖啡脚本中传递给我的多个数组中的最后一个值。我有一些代码可以产生类似的结果,但我无法将它们放在一起。 我在这里定义了一个值数组。

  series = @_parseData {points: @get('points'), series: @get('series')}

这将为我提供一系列数组中的最大值。

  maxvalue = Math.max(answer, (point?.y or 0)) for point in s.data for s in series

这将使我得到一个数组中的最后一个值。

 data = series[0].data
    answer = data[data.length - 1].y

这将得到多个数组中最后一个值的总和。

answer += s.data[s.data.length - 1].y or 0 for s in series

任何帮助将不胜感激。我的语法有问题。

不同系列中的值是为 Y 轴传递的数字,以使一条线位于人力车上。它们应该是诸如 200-15000 之类的数字。每次图表获得更多要绘制的数据时,X 轴基本上只是增加 30。

系列是从这个 ruby​​ 函数传递的。 totinarr 和 totoutarr 只是用作增量计数器的数组。 totin 和 totout 是作为数据值传递的浮点数。

def graphfunction( totin, totinarr, totout, totoutarr, grphname )
  if ( totinarr.last[:y] != totin) | ( totoutarr.last[:y] != totout )

    totinarr.shift
    totinarr.last[:x] += 30
    totinarr << { x: totinarr.last[:x], y: totin }

    totoutarr.last[:y] != totout
    totoutarr.shift
    totoutarr.last[:x] += 30
    totoutarr << {x: totoutarr.last[:x], y: totout }

  end
  totdata = [
    {
      name: "graph1",
      data: totinarr
    },
    {
      name: "graph2",
      data: totoutarr
    }
  ]
  send_event( grphname , series: totdata)
  return true
end

【问题讨论】:

  • 您能提供示例数据吗?变量似乎并不总是指同一件事。
  • 如果我们知道 series 的样子和结果应该是什么,可能会有所帮助。
  • 我添加了一些信息。样本数据将是一个包含 X 和 Y 值的系列(X 只是一个增量,Y 是从 0 到 15000 的编号值)。这有帮助吗?

标签: arrays coffeescript


【解决方案1】:

我能够找出解决方案。这里是。

  series = @_parseData {points: @get('points'), series: @get('series')}
  answer = 0
  answer = s.data[s.data.length - 1].y for s in series when s.data[s.data.length - 1].y > answer
  values = answer

【讨论】:

    猜你喜欢
    • 2012-08-18
    • 2018-02-22
    • 2021-09-01
    • 2013-11-02
    • 2018-06-28
    • 1970-01-01
    • 2021-12-03
    • 2021-07-12
    相关资源
    最近更新 更多