[英]How do I push object elements from GraphQL into nested array in javascript object?
I do currently map through GraphQL data like so:我目前确实通过 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()
]
]
}
}
))
The GraphQL query I use looks like this:我使用的 GraphQL 查询如下所示:
query {
allWordpressWpPlots {
edges {
node {
coordinates {
coord
}
}
}
}
}
..and the output from GraphiQL looks like this: ..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"
}
]
}
}
]
}
}
}
The map function do return an object in the correct format, but my problem is that the "coordinates" node from GrapQL comes in different lengths. map 函数确实以正确的格式返回一个对象,但我的问题是 GrapQL 中的“坐标”节点有不同的长度。 I want to loop through the node using a foreach-loop based on the length of the array, but I get a syntax error when I try to javascript within the map function.
我想使用基于数组长度的 foreach 循环遍历节点,但是当我尝试在 map 函数中使用 javascript 时出现语法错误。
How can I build a "coordinates" array with X amount of object elements from GraphQL?如何使用 GraphQL 中的 X 个对象元素构建“坐标”数组?
You can nest map
calls.您可以嵌套
map
调用。 Not quite sure why coordinates
is an array inside another array, but keeping that as is:不太确定为什么
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.