【发布时间】:2020-06-21 17:18:24
【问题描述】:
我目前确实像这样映射 GraphQL 数据:
const plotDataArray = data.allWordpressWpPlots.edges.map(plot => (
{
geometry: {
type: plot.node.properties.geotype,
coordinates: [
[
plot.node.coordinates[0].coord.split(",").reverse(),
plot.node.coordinates[1].coord.split(",").reverse(),
plot.node.coordinates[2].coord.split(",").reverse(),
plot.node.coordinates[3].coord.split(",").reverse(),
plot.node.coordinates[4].coord.split(",").reverse()
]
]
}
}
))
我使用的 GraphQL 查询如下所示:
query {
allWordpressWpPlots {
edges {
node {
coordinates {
coord
}
}
}
}
}
..GraphiQL 的输出如下所示:
{
"data": {
"allWordpressWpPlots": {
"edges": [
{
"node": {
"coordinates": [
{
"coord": "56.064655444812,9.6949704566207"
},
{
"coord": "56.064575958599,9.6994982706574"
},
{
"coord": "56.06046088577,9.6994719476694 "
},
{
"coord": "56.060440367157,9.6951515896261"
},
{
"coord": "56.064655444812,9.6949704566207"
}
]
}
}
]
}
}
}
map 函数确实以正确的格式返回对象,但我的问题是 GrapQL 中的“坐标”节点的长度不同。我想使用基于数组长度的 foreach 循环遍历节点,但是当我尝试在 map 函数中使用 javascript 时出现语法错误。
如何从 GraphQL 构建一个包含 X 个对象元素的“坐标”数组?
【问题讨论】:
标签: javascript ecmascript-6 graphql