如何用数组值重构约简

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

我正在尝试清理代码。

selectedGroup.items看起来像这样(简化):

[
  { szenarien: [{extent: {xmin: 1, xmax: 2, ymin: 3, ymax: 4}}, ...] },
  { extent: {...}] } // Extent has same properties as above
]

下面是我的映射代码。目标:获取多边形集合的最外点

 const coordinateValues = selectedGroup.items.reduce(
  //TODO: Make this a bit more readable
  (acc, item) => {
    // item is a group
    if (item.szenarios) {
      item.szenarios.map((szenario) => {
        acc.xmin.push(szenario.extent.xmin);
        acc.xmax.push(szenario.extent.xmax);
        acc.ymin.push(szenario.extent.ymin);
        acc.ymax.push(szenario.extent.ymax);
      });
    }
    // item is a szenario
    else {
      acc.xmin.push(item.extent.xmin);
      acc.xmax.push(item.extent.xmax);
      acc.ymin.push(item.extent.ymin);
      acc.ymax.push(item.extent.ymax);
    }
    return acc;
  },
  { xmin: [], xmax: [], ymin: [], ymax: [] }
);

// Prepare an extent with the smallest xmin, ymin and the biggest xmax, ymax to have all szenarios in it
const calculatedGroupExtent: __esri.Extent = new EsriExtent({
  xmax: Math.max(...coordinateValues.xmax) + 200,
  xmin: Math.min(...coordinateValues.xmin) - 200,
  ymax: Math.max(...coordinateValues.ymax) + 200,
  ymin: Math.min(...coordinateValues.ymin) - 200,
  spatialReference: { wkid: 2056 },
});

显然,它不是真正可读的。如果没有添加许多“可读性常量”(因此没有任何改进),我找不到一种简单的方法可以简化它。

javascript refactoring gis arcgis-js-api
2个回答
0
投票

您可以创建一个辅助函数,该函数需要一个szenario对象并将每个['xmin', 'xmax', 'ymin', 'ymax']属性推入适当的数组。然后,在遍历项目时,您只需测试项目是否为组(在这种情况下,您将执行item.szenarios.forEach(addSzenario)),否则添加单个szenario:addSzenario(item.extent)

const props = ['xmin', 'xmax', 'ymin', 'ymax'];
const coordinateValues = Object.fromEntries(
  props.map(prop => [prop, []])
);
const addSzenario = (szenario) => {
  for (const prop of props) {
    coordinateValues[prop].push(szenario[prop]);
  }
};
for (const item of selectedGroup.items) {
  // item is a group
  if (item.szenarios) {
    item.szenarios.forEach(addSzenario);
  } else {
    addSzenario(item.extent);
  }
}

0
投票

您可以使用reduce来计算最大值和最小值。

const getVal = (item) => {
  return item.szenarios ? szenario.extent[key] : item.extent[key];
}

const getMin = (group, key) => {
  return group.items.reduce((acc, item) => {
    return Math.min(acc, getVal(item));
  }, Number.MAX_SAFE_INTEGER); 
}

const getMax = (group, key) => {
  return group.items.reduce((acc, item) => {
    return Math.max(acc, getVal(item));
  }, Number.MIN_SAFE_INTEGER); 
}


const xmin = getMin(selectedGroup, "xmin");
const xmax = getMin(selectedGroup, "xmax");
const ymin = getMin(selectedGroup, "ymin");
const ymax = getMin(selectedGroup, "ymax");
© www.soinside.com 2019 - 2024. All rights reserved.