如何将对象元素从GraphQL推送到javascript对象中的嵌套数组中?
我目前确实通过GraphQL数据进行映射,如下所示:如何将对象元素从GraphQL推送到javascript对象中的嵌套数组中?,javascript,ecmascript-6,graphql,Javascript,Ecmascript 6,Graphql,我目前确实通过GraphQL数据进行映射,如下所示: const plotDataArray = data.allWordpressWpPlots.edges.map(plot => ( { geometry: { type: plot.node.properties.geotype, coordinates: [ [ plot.node.coordinates[0].coord.
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
}
}
}
}
}
{
"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"
}
]
}
}
]
}
}
}
..GraphiQL的输出如下所示:
query {
allWordpressWpPlots {
edges {
node {
coordinates {
coord
}
}
}
}
}
{
"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
调用。不太清楚为什么坐标
是另一个数组中的数组,但保持原样:
const plotDataArray = data.allWordpressWpPlots.edges.map(plot => ({
geometry: {
type: plot.node.properties.geotype,
coordinates: [
plot.node.coordinates.map(coordinate => {
return coordinate.coord.split(",").reverse()
}),
]
},
}))