javascript - Get some information on a string with split and substring -
i have information of latitude , longitude string filepos. have put information array(called beaches), must be:
var beaches = [ ["45.411158", "11.906326", 1]// 1 index ["45.411190", "11.906324", 2]// 2 index ]
i tried that, eclipse show me error:
"uncaught typeerror: cannot read property 'length' of undefined"
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 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));
if change object string valid json , parse it, can iterate on object.
var jsonstring = filepos.replace(/\'/g, '"').split('}'), positionary; jsonstring.pop(); positionary = json.parse('[' + jsonstring.join('},') + '}]'); beaches = positionary.map(function (obj) { return [obj.lat, obj.lng]; });
Comments
Post a Comment