1

I have this string:

var filePos = "{'Data':'17/02/2015', 'Descrizione':'PROVA AAA', 'Lat':'45.411258', 'Lng':'11.906326', 'Foto':'sdjahvas'}" +
        "{'Data':'18/02/2015', 'Descrizione':'PROVA BAA', 'Lat':'45.411190', 'Lng':'11.906324', 'Foto':'asde'}";

I have to put in array some information like Data, Lat, Lng, Index(is progressive).

I use this function:

var jsonString = filePos.replace(/\'/g, '"').split('}'),
positionAry;
jsonString.pop();
positionAry = JSON.parse('[' + jsonString.join('},') + '}]');
beaches = positionAry.map(function (obj, index) {
return [obj.Data, obj.Lat, obj.Lng, index+1];
});

but the array that return is:

var beaches = [
               ["17/02/2015", "45.411158", "11.906326", 1],
               ["18/02/2015", "45.411190", "11.906324", 2],
             ];

So I don't want this array, but this:

var beaches = [
                   ["17/02/2015", 45.411158, 11.906326, 1],
                   ["18/02/2015", 45.411190, 11.906324, 2],
                 ];

In short, i don't want Lat and Lng in inverted commas. How can i do that?

1
  • 1
    Try this. return [obj.Data, parseFloat(obj.Lat), parseFloat(obj.Lng), index+1]; Commented Jun 29, 2015 at 8:28

2 Answers 2

5

In your JSON string filePos , those values are represented as strings, not as numbers.

If you don't want the "inverted commas" (ie. you don't want them as strings), you have to manually parse them as numbers, using parseFloat().

Sign up to request clarification or add additional context in comments.

Comments

2

Parsing a string results in substrings. To get the numeric values for lat and long you need to use parsefloat on the substrings.

parseFloat(obj.lat)

hope this points you into the right direction

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.