-2

I have a collection of data like this -

[
 0: {latitude: "0", longitude: "0", user_country_name: "-", user_country_code: "-", total_visitors: 4}
 1: {latitude: "-33.867851", longitude: "151.207321", user_country_name: "Australia", user_country_code: "AU", total_visitors: 1}
 2: {latitude: "-23.960831", longitude: "-46.333611", user_country_name: "Brazil", user_country_code: "BR", total_visitors: 1}
 3: {latitude: "45.411171", longitude: "-113.468712", user_country_name: "Canada", user_country_code: "CA", total_visitors: 2}
 4: {latitude: "47.366669", longitude: "8.55", user_country_name: "Switzerland", user_country_code: "CH", total_visitors: 1}
]

I get the result by using

var location_data = [];
  $.ajax({
    type: "GET",
    url: url,
    data: data, 
    success: function(data)
    {
        var obj = JSON.parse(data);
        obj.forEach(function(item) {
          var data = { latLng: '['+parseFloat(item.latitude)+', '+parseFloat(item.longitude)+']', name: item.user_country_name }

          location_data.push(data);

        })
    } 
  });

Output :

[
 0: {latLng: "[0, 0]", name: "-"}
 1: {latLng: "[-33.867851, 151.207321]", name: "Australia"}
 2: {latLng: "[-23.960831, -46.333611]", name: "Brazil"}
 3: {latLng: "[45.411171, -113.468712]", name: "Canada"}
 4: {latLng: "[47.366669, 8.55]", name: "Switzerland"}
]

But I want something like the following, I mean display the item of the object like [-33.867851, 151.207321] not wrapped with ""quotes;

Expected Output:

[
     0: {latLng: [0,0], name: "-"}
     1: {latLng: [-33.867851, 151.207321], name: "Australia"}
     2: {latLng: [-23.960831, -46.333611], name: "Brazil"}
     3: {latLng: [45.411171, -113.468712], name: "Canada"}
     4: {latLng: [47.366669, 8.55], name: "Switzerland"}
]
  • 2
    `latLng: '['+parseFloat(item.latitude)+', '+parseFloat(item.longitude)+']'` Don't _generate it as string_ in the first place. `latLng: [parseFloat(item.latitude), parseFloat(item.longitude)]` – tkausl Apr 17 '21 at 07:36
  • [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas Apr 17 '21 at 07:44
  • @Nick None of these answers solved my problem. And I have to fix it from the PHP end. – Masudul Hasan Shawon May 05 '21 at 05:28
  • You cannot fix this from the PHP end as that is not where the problem is. The problem is in your JS code, which is what the answers show you how to fix. – Nick May 05 '21 at 07:13
  • @Nick, since I couldn't figure out what is the actual problem in js and how to fix it, I posted it here and asked for a solution. But since I didn't get a solution here I have to focus on my PHP code and passed a custom array to lat long field. – Masudul Hasan Shawon May 05 '21 at 07:31

1 Answers1

0

You can simply parse each item's latitude, longitute properties to numbers and add them as the 2 elements of a latLng array.

Array.map is the most appropriate way to iterate and transform.

const arr = [
  { latitude: "-33.23", longitude: "44.22", user_country_name: "Italy", user_country_code: "-", total_visitors: 4},
  { latitude: "-15.25", longitude: "66.55", user_country_name: "Spain", user_country_code: "-", total_visitors: 4}
]

const output = arr.map(item => {
  return {
    name: item.user_country_name,
    latLng: [+item.latitude, +item.longitude]
  }
})

console.log(output)
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167