【发布时间】:2022-08-10 00:05:28
【问题描述】:
假设我有一些这样的数据:
[ {...otherData, coordinates: {x: 2022, y: 149832}}, {...otherData, coordinates: {x: 2023, y: 153729}}, {...otherData, coordinates: {x: 2024, y: 156800}}, {...otherData, coordinates: {x: 2024, y: 195233}}, {...otherData, coordinates: {x: 2035, y: 341978}} ]
我想使用 x 和 y 键将最相似的值组合在一起。输出看起来像这样:
[ [{...otherData, coordinates: {x: 2022, y: 149832}}, {...otherData, coordinates: {x: 2023, y: 153729}}, {...otherData, coordinates: {x: 2024, y: 156800}}], [ {...otherData, coordinates: {x: 2024, y: 195233}}, {...otherData, coordinates: {x: 2035, y: 341978}}] ]
返回的数组将包含嵌套数组,每个数组都包含分组数据,最后一个数组包含无法分组的数据。
要设置相似的范围,假设 x 值必须彼此相差 1 年。如果该检查通过,则后续检查是针对 y 值。它们之间的距离必须在 10,000 以内。我曾考虑过为此使用reduce,但老实说,我一直对逻辑的样子一无所知。
给好奇的人解释一下:
这背后的目的是最终我将在散点图上使用它们。问题是传入的数据将具有最终重叠在点簇中的点。而不是一个集群,我希望聚合相似的数据值,并在这些组中,选择具有最低 y 值的那个,并将其绘制到图表上。当您点击该绘图点时,将出现一个包含该组剩余值的小图形。
标签: javascript typescript lodash