[英]How do I push object elements from GraphQL into nested array in javascript object?
我目前確實通過 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 個對象元素構建“坐標”數組?
您可以嵌套map
調用。 不太確定為什么coordinates
是另一個數組中的數組,但保持原樣:
const plotDataArray = data.allWordpressWpPlots.edges.map(plot => ({
geometry: {
type: plot.node.properties.geotype,
coordinates: [
plot.node.coordinates.map(coordinate => {
return coordinate.coord.split(",").reverse()
}),
]
},
}))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.