1

I have thousands of lines of code like this:

var coordinates = [
    new google.maps.LatLng(11.11111, 22.222222),
    new google.maps.LatLng(33.33333, 44.444444),
    new google.maps.LatLng(55.55555, 66.666666),

    //Thousands of lines
];

I need to format code above to array of LatLng arrays containing Lat and Lng separately:


[0] => [lat] => [11.11111], [lng] => [22.22222]
[1] => [lat] => [33.33333], [lng] => [44.44444]
[2] => [lat] => [55.55555], [lng] => [66.66666]

//etc

Doing it one by one on my own would take forever. I need to hardcode the outcome to my code which means it would be best if I could build a frontend converter with (2) textarea(s) where I could copy my 1st code, press button and get the results which I could copy-paste to my own code.

PHP and JS both works for me, choose the language you like.

How to get numbers from my 1st block of code and build the structure in my second code?

1 Answer 1

1

You can use Array.prototype.map() to transform your array:

var result = coordinates.map(function(point) {
    return {
        lat: point.lat(),
        lng: point.lng()
    };
});

// access it
console.log(result[0].lat);
// or
console.log(result[0]['lat'];
Sign up to request clarification or add additional context in comments.

2 Comments

This works perfectly. However, is there a way to name arrays? I currently get Object-s that contain lat and lng, I would love to rename it.
@Solo what do you mean - rename object?

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.