用JavaScript映射嵌套数组
我正在尝试使用.map映射嵌套数组,以便在地图中显示并精确定位所有伦敦地铁位置用JavaScript映射嵌套数组,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我正在尝试使用.map映射嵌套数组,以便在地图中显示并精确定位所有伦敦地铁位置 this.undergroundGeoJson = [ { 'type': 'FeatureCollection', 'crs': { 'type': 'name', 'properties': { 'name': 'urn:ogc:def:crs:OGC:1.3:CRS84' } }, 'featu
this.undergroundGeoJson = [
{
'type': 'FeatureCollection',
'crs': { 'type': 'name', 'properties': { 'name':
'urn:ogc:def:crs:OGC:1.3:CRS84' } },
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [-0.278126, 51.5025]
},
'properties': {
'name': 'Acton Town'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [-0.263033174, 51.50883531]
},
'properties': {
'name': 'Acton Central'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [-0.262879534, 51.50856013]
},
'properties': {
'name': 'Acton Central'
}
}
}
]
我需要几何体obj中的坐标数组元素
这是我到目前为止的代码
@computed
get undergroundLatLongs() {
return this.undergroundGeoJson.map((u) =>
[u.features.geometry.coordinates[0],
u.features.geometry.coordinates[1]]);
}
这是错误日志
Uncaught TypeError: Cannot read property 'coordinates' of undefined
欢迎任何帮助 您试图在undergroundGeoJson对象上使用.map。map只能用于数组。我相信您正在尝试遍历this.underroundgeojson.features中的对象数组?您必须这样做:
this.undergroundGeoJson.features.map(u => {
console.log(u) // this will print each object within `features`
return u; // don't forget to return something
})
您试图在undergroundGeoJson对象上使用.map。map只能用于数组。我相信您正在尝试遍历this.underroundgeojson.features中的对象数组?您必须这样做:
this.undergroundGeoJson.features.map(u => {
console.log(u) // this will print each object within `features`
return u; // don't forget to return something
})
由于映射是在数组中循环工作的,因此可以从此.UnderroundGeoJSON[0]功能启动映射 this.underroundgeoJSON=[{ “类型”:“功能集合”, “crs”:{ “类型”:“名称”, “属性”:{ “名称”:“urn:ogc:def:crs:ogc:1.3:CRS84” } }, “特征”:[{ “类型”:“特征”, “几何学”:{ '类型':'点', “坐标”:[-0.278126,51.5025] }, “属性”:{ “名称”:“阿克顿镇” } }, { “类型”:“特征”, “几何学”:{ '类型':'点', “坐标”:[-0.263033174,51.50883531] }, “属性”:{ 'name':'Acton Central' } }, { “类型”:“特征”, “几何学”:{ '类型':'点', “坐标”:[-0.262879534,51.50856013] }, “属性”:{ 'name':'Acton Central' } } ] }] 地下车道的功能{ 返回此。UnderroundGeoJSON[0]。features.mapu=> [u.几何坐标[0], u、 几何。坐标[1]]; } var x=地下车道;
console.logx 由于映射是在数组中循环工作的,因此可以从此.UnderroundGeoJSON[0]功能启动映射 this.underroundgeoJSON=[{ “类型”:“功能集合”, “crs”:{ “类型”:“名称”, “属性”:{ “名称”:“urn:ogc:def:crs:ogc:1.3:CRS84” } }, “特征”:[{ “类型”:“特征”, “几何学”:{ '类型':'点', “坐标”:[-0.278126,51.5025] }, “属性”:{ “名称”:“阿克顿镇” } }, { “类型”:“特征”, “几何学”:{ '类型':'点', “坐标”:[-0.263033174,51.50883531] }, “属性”:{ 'name':'Acton Central' } }, { “类型”:“特征”, “几何学”:{ '类型':'点', “坐标”:[-0.262879534,51.50856013] }, “属性”:{ 'name':'Acton Central' } } ] }] 地下车道的功能{ 返回此。UnderroundGeoJSON[0]。features.mapu=> [u.几何坐标[0], u、 几何。坐标[1]]; } var x=地下车道; console.logx features是一个数组,您需要使用索引访问它 const undergroundGeoJson=[{'type':'FeatureCollection','crs':{'type':'name','properties':{'name':'urn:ogc:def:crs:ogc:1.3:CRS84'},'features':[{'type':'Feature','geometry':{'type':'Point','Coordinations':[-0.278126,51.5025]},'properties':{'name':'ActonTown'},{'type':'Feature':'Feature','geometry':'Point:{'353551',','properties':{'name':'ActonCentral'},{'type':'Feature','geometry':{'type':'Point','coordinates':[-0.262879534,51.50856013],'properties':{'name':'ActonCentral'},}]; const ret=underroundgeoJSON.mapu,i=>[ u、 特征[i]。几何体。坐标[0], u、 特征[i].几何.坐标[1], ]; console.logret features是一个数组,您需要使用索引访问它 const undergroundGeoJson=[{'type':'FeatureCollection','crs':{'type':'name','properties':{'name':'urn:ogc:def:crs:ogc:1.3:CRS84'},'features':[{'type':'Feature','geometry':{'type':'Point','Coordinations':[-0.278126,51.5025]},'properties':{'name':'ActonTown'},{'type':'Feature':'Feature','geometry':'Point:{'353551',','properties':{'name':'ActonCentral'},{'type':'Feature','geometry':{'type':'Point','coordinates':[-0.262879534,51.50856013],'properties':{'name':'ActonCentral'},}]; const ret=underroundgeoJSON.mapu,i=>[ u、 特征[i]。几何体。坐标[0], u、 特征[i].几何.坐标[1], ];
console.logret 您试图访问阵列要素的属性几何体,但这是错误的 所以你必须像这样绘制地图
u.features.map(f => f.geometry.coordinates[0])
您的最终代码应该是这样的
this.underroundgeoJSON=[{
“类型”:“功能集合”,
“crs”:{
“类型”:“名称”,
“属性”:{
“名称”:“urn:ogc:def:crs:ogc:1.3:CRS84”
}
},
“特征”:[{
“类型”:“特征”,
“几何学”:{
'类型':'点',
“坐标”:[-0.278126,51.5025]
},
“属性”:{
“名称”:“阿克顿镇”
}
},
{
“类型”:“特征”,
'g
“几何测量法”:{
'类型':'点',
“坐标”:[-0.263033174,51.50883531]
},
“属性”:{
'name':'Acton Central'
}
},
{
“类型”:“特征”,
“几何学”:{
'类型':'点',
“坐标”:[-0.262879534,51.50856013]
},
“属性”:{
'name':'Acton Central'
}
}
]
}]
地下车道的功能{
返回this.underroundgeoJSON.mapu=>
[u.features.mapf=>f.geometry.coordinates[0],
u、 features.mapf=>f.geometry.coordinates[1];
}
console.logunderroundlatlongs 您试图访问阵列要素的属性几何体,但这是错误的 所以你必须像这样绘制地图
u.features.map(f => f.geometry.coordinates[0])
您的最终代码应该是这样的
this.underroundgeoJSON=[{
“类型”:“功能集合”,
“crs”:{
“类型”:“名称”,
“属性”:{
“名称”:“urn:ogc:def:crs:ogc:1.3:CRS84”
}
},
“特征”:[{
“类型”:“特征”,
“几何学”:{
'类型':'点',
“坐标”:[-0.278126,51.5025]
},
“属性”:{
“名称”:“阿克顿镇”
}
},
{
“类型”:“特征”,
“几何学”:{
'类型':'点',
“坐标”:[-0.263033174,51.50883531]
},
“属性”:{
'name':'Acton Central'
}
},
{
“类型”:“特征”,
“几何学”:{
'类型':'点',
“坐标”:[-0.262879534,51.50856013]
},
“属性”:{
'name':'Acton Central'
}
}
]
}]
地下车道的功能{
返回this.underroundgeoJSON.mapu=>
[u.features.mapf=>f.geometry.coordinates[0],
u、 features.mapf=>f.geometry.coordinates[1];
}
console.logunderroundlatlongs;提供您想要的示例输出是数组,而不是对象,因此不能对其使用对象表示法。它应该类似于features[0]。正如Jayce444所指出的,您需要在features数组中建立索引。请提供示例输出,说明您想要的是数组,而不是对象,因此您不能对其使用对象表示法。它应该类似于功能[0]。geometryAs由Jayce444指出,您需要功能数组中的索引。这段代码是否可以回答这个问题,对您更改的内容的解释将非常有助于OPYou are映射函数返回未定义的值,您只需要consol.log,为什么不尝试forEach?@Treycos按照建议进行更改@按建议做了一些改变!这段代码可以回答这个问题吗?对您所更改内容的解释将非常有助于OPYou are映射函数返回未定义,您只需要consol.log,为什么不试试forEach?@Treycos根据建议进行了更改@按建议做了一些改变!谢谢你,它确实起作用了。然而,我只得到了第一组坐标。知道为什么吗?谢谢你,它确实有用。然而,我只得到了第一组坐标。知道为什么吗?