You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将带坐标信息的对象数组转换为[Lat,Long]格式的嵌套列表数组

代码存在的问题
  • 外层map回调没有返回值:内层Coordinates.map的执行结果没有被外层返回,最终得到的arrList所有元素都是undefined
  • 字段读取层级错误:你尝试从外层Location对象coords上读取Lat和Long,但这两个属性实际是Coordinates数组子元素coord的属性
  • 没有缺失字段兼容:部分对象不存在Coordinates字段,直接访问会抛出读取map方法的异常
正确实现

如果需要过滤掉没有坐标的对象,用以下代码:

const arrList = resp.data.data
  .filter(item => item.Coordinates && Array.isArray(item.Coordinates))
  .map(item => item.Coordinates.map(coord => [coord.Lat, coord.Long]))
console.log("ArrList:", arrList);

如果需要保留无坐标对象对应的位置,用空数组占位,用以下代码:

const arrList = resp.data.data.map(item => {
  if (!item.Coordinates || !Array.isArray(item.Coordinates)) return []
  return item.Coordinates.map(coord => [coord.Lat, coord.Long])
})
console.log("ArrList:", arrList);

内容的提问来源于stack exchange,提问作者Sai Krishnadas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 07:21:04