我必须从字符串filePos获取纬度和经度的信息。 我必须将这些信息放入一个数组(称为海滩),所以它必须是:
var beaches = [
["45.411158", "11.906326", 1]// 1 is the index
["45.411190", "11.906324", 2]// 2 is the index
]
我试图这样做,但是eclipse向我显示了这个错误:
“未捕获的TypeError:无法读取未定义的属性'长度'”
var filePos = "{'Data':'17/02/2015', 'Descrizione':'PROVA AAA', 'Lat':'45.411158', 'Lng':'11.906326', 'Foto':'sdjahvas'}" +
"{'Data':'18/02/2015', 'Descrizione':'PROVA BAA', 'Lat':'45.411190', 'Lng':'11.906324', 'Foto':'asde'}";
var beaches = filePos.split("}") // Break up the original string on `", "`
.map(function(element, index){
var temp = element.split(', ');
lat = temp[2].substr(7, temp[2].length -2);
lng = temp[3].substr(7, temp[3].length -2);
return [lat, lng, index + 1];
});
console.log(beaches);
alert(JSON.stringify(beaches));
答案 0 :(得分:1)
如果将对象字符串更改为有效的json并对其进行解析,则可以迭代该对象。
var jsonString = filePos.replace(/\'/g, '"').split('}'),
positionAry;
jsonString.pop();
positionAry = JSON.parse('[' + jsonString.join('},') + '}]');
beaches = positionAry.map(function (obj) {
return [obj.Lat, obj.Lng];
});