Obtengo una cadena de la base de datos y necesito convertirla en el siguiente formato:
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] ];mi código a continuación
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("|")solo obtengo caracteres cuando paso el código a continuación
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);La matriz de ubicación codificada funciona bien, pero cuando uso mi ciclo no obtengo los datos correctos. Por favor, ayuda.
me parece que no tienes una cadena como entrada sino una matriz de objetos
puede usar el map para transformar esa matriz en la forma que desee
como esto
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])o puede usar esa matriz de objetos para completar su mapa
como esto
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); })a partir de su código, parece que solo desea convertir un objeto en una matriz que contiene matrices. No veo la razón por la que trabajar con cadenas aquí.
var result = Object.keys(obj).map((key) => { return [obj[key].Location, obj[key].x, obj[key].y] })