0

I'm getting string from database and i need to convert it in the format below:

var locations = [
  ["LOCATION_1", 11.8166, 122.0942],
  ["LOCATION_2", 11.9804, 121.9189],
  ["LOCATION_3", 10.7202, 122.5621],
  ["LOCATION_4", 11.3889, 122.6277],
  ["LOCATION_5", 10.5929, 122.6325]
];

my code below

                   var array = ''
                   var arrayprev= ''
                   for (var i = 0; i < obj.length; i++) {
                        array = '['+obj[i].Location + '"' + ',' + obj[i].x + ',' + obj[i].y + '],|'
                        if (arrayprev == "") {
                            arrayprev = array;
                        }
                        else {
                            arrayprev = arrayprev + array;
                        }
                    }
                   locations = arrayprev.split("|")

i'm getting only chracters when passing in code below

for (var i = 0; i < locations.length; i++) {
        marker = new L.marker([locations[i][1], locations[i][2]], { icon: iconColor(color) })
            // PoPup result will be displayed here
          .bindPopup(locations[i][0])
            //here tool tip will be used to display the label text
          .bindTooltip(locations[i][0],
            {
                permanent: true, 
                direction: 'right'
             }
            )
          .addTo(map);

hardcoded location array is working fine but when I use my loop I'm not getting the correct data Please help

4
  • can you add an input string example? Commented Jun 9, 2022 at 14:06
  • from this line i'm taking data array = '['+obj[i].Location + '"' + ',' + obj[i].x + ',' + obj[i].y + '],|' Commented Jun 9, 2022 at 14:11
  • what is the initial string, which you want to convert to an array is the question. Commented Jun 9, 2022 at 14:50
  • you have variables called array, which are actually strings. then you build one big string and split it again to have an array. it all seems quite confusing Commented Jun 9, 2022 at 14:54

2 Answers 2

1

it seems to me that you don't have a string as an input but an array of object

you can use map to trasform that array in the shape you want

like this

const obj = [{
  Location: "LOCATION_1",
  x:  11.8166,
  y: 122.0942
},{
  Location: "LOCATION_2",
  x:  11.8166,
  y: 122.0942
},{
  Location: "LOCATION_3",
  x:  11.8166,
  y: 122.0942
},
]

const locations = obj.map(({Location, x, y}) => [Location, x, y])

or you can use that array of object to populate your map

like this

obj.forEach(({Location, x, y}) => {
  marker = new L.marker([x, y], { icon: iconColor(color) })
            // PoPup result will be displayed here
          .bindPopup(Location)
            //here tool tip will be used to display the label text
          .bindTooltip(Location,
            {
                permanent: true, 
                direction: 'right'
             }
            )
          .addTo(map);

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

Comments

0

from your code, it looks like you just want to convert an object into an array containing arrays. I don't see the reason working with string here.

var result = Object.keys(obj).map((key) => {
    return [obj[key].Location, obj[key].x, obj[key].y]
})

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.