从 geoJSON 到数组
From geoJSON to Array
真正的问题是我似乎无法设置坐标内的值。
JSON 的开头看起来像这样。
{
"path":{
"type":"FeatureCollection",
"features":[
{
"geometry":{
"type":"LineString",
"coordinates":[
[
10.4048129700337,
63.416688738540316
],
[
10.404757623420094,
63.4167327334674
]
]
},
"type":"Feature",
"id":85624,
"properties":{
"flags":[
],
"z":2.0,
"m":5.62957707088913e-06,
"target":85621,
"buildingId":43
}
}
当我 运行 它给了我代码时
"Uncaught TypeError: Cannot set property '0' of undefined" on
remakeObj[i].coordinates[j][k] = coordinates[j][k];
现在已经解决了。我是这样解决的
parseToObjects: function (data) {
var remakeObj = [];
var features = data.path.features;
for (var i in features) {
remakeObj[i] = {
flooring: "",
meters: "",
coordinates: []
};
var coordinates = features[i].geometry.coordinates;
for (var j in coordinates) {
remakeObj[i].coordinates[j] = [];
for (var k in coordinates[j]) {
remakeObj[i].coordinates[j][k] = coordinates[j][k];
remakeObj[i].flooring = features[i].properties.z;
remakeObj[i].meters = features[i].properties.m;
}
}
}
console.log(remakeObj);
return remakeObj;
}
JavaScript
中的数组实际上是 hash tables(可以把它想象成包含值和键以便以某种方式访问它的桶)而不是序列号索引的数组。
虽然允许使用数字索引作为键,但数组也可以包含其他类型的键(string
类型等)。
因此,索引0
不会return数组的第一个元素,而是存储myArray[0]
的值元素,如果有的话(在你的情况下,没有任何这就是您收到错误的原因)。
您需要使用设置的键迭代数组,以便连续访问所有值:
for (var key in myArray) {
var y = myArray[key]; //Index it like this, use it similarly.
}
更新:这是你可能做的(警告:未经测试的代码):
for (var featuresKey in features) {
remakeObj[featuresKey] = [features.length];
var coordinates = features[featuresKey].geometry.coordinates;
remakeObj[featuresKey].coordinates = new Array(coordinates.length);
for (var coordinatesKey in coordinates) {
for (var innerCoordinatesKey in coordinates[coordinatesKey]) {
remakeObj[featuresKey].coordinates[coordinatesKey][innerCoordinatesKey] =
coordinates[coordinatesKey][innerCoordinatesKey];
remakeObj[featuresKey].flooring = features[featuresKey].properties.z;
remakeObj[featuresKey].meters = features[featuresKey].properties.m;
}
}
}
真正的问题是我似乎无法设置坐标内的值。
JSON 的开头看起来像这样。
{
"path":{
"type":"FeatureCollection",
"features":[
{
"geometry":{
"type":"LineString",
"coordinates":[
[
10.4048129700337,
63.416688738540316
],
[
10.404757623420094,
63.4167327334674
]
]
},
"type":"Feature",
"id":85624,
"properties":{
"flags":[
],
"z":2.0,
"m":5.62957707088913e-06,
"target":85621,
"buildingId":43
}
}
当我 运行 它给了我代码时
"Uncaught TypeError: Cannot set property '0' of undefined" on remakeObj[i].coordinates[j][k] = coordinates[j][k];
现在已经解决了。我是这样解决的
parseToObjects: function (data) {
var remakeObj = [];
var features = data.path.features;
for (var i in features) {
remakeObj[i] = {
flooring: "",
meters: "",
coordinates: []
};
var coordinates = features[i].geometry.coordinates;
for (var j in coordinates) {
remakeObj[i].coordinates[j] = [];
for (var k in coordinates[j]) {
remakeObj[i].coordinates[j][k] = coordinates[j][k];
remakeObj[i].flooring = features[i].properties.z;
remakeObj[i].meters = features[i].properties.m;
}
}
}
console.log(remakeObj);
return remakeObj;
}
JavaScript
中的数组实际上是 hash tables(可以把它想象成包含值和键以便以某种方式访问它的桶)而不是序列号索引的数组。
虽然允许使用数字索引作为键,但数组也可以包含其他类型的键(string
类型等)。
因此,索引0
不会return数组的第一个元素,而是存储myArray[0]
的值元素,如果有的话(在你的情况下,没有任何这就是您收到错误的原因)。
您需要使用设置的键迭代数组,以便连续访问所有值:
for (var key in myArray) {
var y = myArray[key]; //Index it like this, use it similarly.
}
更新:这是你可能做的(警告:未经测试的代码):
for (var featuresKey in features) {
remakeObj[featuresKey] = [features.length];
var coordinates = features[featuresKey].geometry.coordinates;
remakeObj[featuresKey].coordinates = new Array(coordinates.length);
for (var coordinatesKey in coordinates) {
for (var innerCoordinatesKey in coordinates[coordinatesKey]) {
remakeObj[featuresKey].coordinates[coordinatesKey][innerCoordinatesKey] =
coordinates[coordinatesKey][innerCoordinatesKey];
remakeObj[featuresKey].flooring = features[featuresKey].properties.z;
remakeObj[featuresKey].meters = features[featuresKey].properties.m;
}
}
}