简体   繁体   English

在 JavaScript 中将字符串转换为二维数组格式

[英]convert string into 2D-array format in JavaScript

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硬编码的位置数组工作正常,但是当我使用我的循环时,我没有得到正确的数据请帮助

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您可以使用map将该数组转换为您想要的形状

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);

})

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]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM