【发布时间】:2018-12-11 01:46:54
【问题描述】:
我有一个 3 维数组,其中包含 n 个多边形集合的最小/最大纬度边界集。我想从所有多边形的集合中找到最小和最大坐标。
我下面的解决方案有效,但我觉得它很笨拙。我真正的问题是:有没有办法获得沿轴的最小值/最大值,以便它返回[ [lat_min, lon_min], [lat_max, lon_max] ] 形式的数组,而无需分别对每个点执行reduce 函数?
// Where bounds is an array of latlon bounds for n polygons:
// bounds = [
// [ [min_lat_1, min_lon_1], [max_lat_1, max_lon_1] ],
// ...
// [ [min_lat_n, min_lon_n], [max_lat_n, max_lon_n] ]
// ]
const x1 = bounds.reduce((min, box) => {
return box[0][0] < min ? box[0][0] : min;
}, bounds[0][0][0]);
const y1 = bounds.reduce((min, box) => {
return box[0][1] < min ? box[0][1] : min;
}, bounds[0][0][1]);
const x2 = bounds.reduce((max, box) => {
return box[1][0] > max ? box[1][0] : max;
}, bounds[0][1][0]);
const y2 = bounds.reduce((max, box) => {
return box[1][1] > max ? box[1][1] : max;
}, bounds[0][1][1]);
编辑:到目前为止,我得到的响应改进了我的代码,但到目前为止,没有什么能完全达到我的期望。
一些进一步的背景/规范:我更熟悉 python/numpy,您可以在其中指定跨任何轴应用函数。在这种情况下,我想沿轴 3(即深度轴)应用我的函数。但是,由于我不只是在寻找最小值/最大值,因此我创建的函数还需要根据索引返回一个函数(最小值或最大值)。这在Javascript中根本不可行吗?似乎 es6 中应该有一些优雅的组合来完成工作。
【问题讨论】:
标签: javascript arrays ecmascript-6